2017-05-31 13 views
0

我必须为我的应用程序实现文本消息功能,因为我有一个PHP文件,我从服务提供商那里需要放置数据库details.and并将其路径给服务提供商添加到服务器中。我很困惑在哪里放置,并根据他,如果我在URL中给文件路径,它应该返回联系电话号码和消息正文,但我无法测试它。混淆在哪里放置php文件(laravel 5.3)

脚本看起来

<?php 
    // MySQL table outbox: 
    // CREATE TABLE outbox(sender VARCHAR(255), rcpt VARCHAR(255), body VARCHAR(255)); 
    $mysql_host = "localhost"; 


    $mysql_base = "school_laravel"; 

    $mysql_user = "root"; 

    $mysql_password = ""; 

    $table = "outbox"; 

    mysql_connect($mysql_host, $mysql_user, $mysql_password); 
    mysql_select_db($mysql_base); 

    mysql_query("LOCK TABLES $table WRITE, $table AS $table" . "_read READ"); 

$get_query = "SELECT * FROM $table AS $table" . "_read"; 
$del_query = "DELETE FROM " . $table; 
if ($_GET['device'] != '') { 
    $suffix = " WHERE sender='" . $_GET['device'] . "'"; 
    $get_query .= suffix; 
    $del_query .= suffix; 
} 
$result = mysql_query($get_query); 
    echo '<messages>'; 
    while ($array = mysql_fetch_array($result)) { 
    echo '<message msisdn="' . $array['rcpt'] . '">' . $array['body'] . "</message>\n"; 
} 
mysql_query($del_query); 
mysql_query("UNLOCK tables"); 
    echo '</messages>'; 
?> 

我的问题是,我应该在哪里把它放在laravel目录,并有可能正如我所提到的上述脚本DB细节得到一个表的详细信息。

感谢您的帮助

+3

请勿使用'mysql_ *'功能。自v5.5(2013年6月)开始,它们已被弃用,并从v7.0(2015年12月)开始删除。请使用[** mysqli _ ***](https://secure.php.net/manual/en/book.mysqli.php)或[** PDO **](https://secure.php.net /manual/en/book.pdo.php)与[**准备语句**](https://secure.php.net/manual/en/pdo.prepare.php)和[**绑定参数** ](https://secure.php.net/manual/en/pdostatement.bindparam.php)。 –

+1

您的代码易受[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻击。你应该使用[** mysqli **](https://secure.php。net/manual/en/mysqli.prepare.php)或者[** PDO **](https://secure.php.net/manual/en/pdo.prepared-statements.php)准备了具有所述绑定参数的语句在[**这篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)。 –

+0

请任何人都可以给它作为答案使用PDO或其他东西 –

回答

4

即使它可以节省你一分钟,我不会用在您的网站的代码。除非您重写它,否则很难在Laravel站点上使用。如果你不得不重写它,也许你可以用Laravel的方式来完成它。

您看起来相当直接的重写脚本。此代码未经测试。我不希望将其复制并粘贴到您的应用程序中,并且无需修改即可正常工作,但这里是基本步骤。

步骤1 - Create a Model for your table.

应用程序/ Outbox.php

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Outbox extends Model 
{ 
    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'outbox'; 

} 

第2步 - Create a controller

应用程序/ HTTP /控制器/ MessagesController.php

<?php 

namespace App\Http\Controllers; 

use App\Outbox; 
use Illuminate\Http\Request; 

class MessagesController extends Controller 
{ 
    /** 
    * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 
    */ 
    public function show($sender) 
    { 

     $messages = Outbox::where('sender', $sender) 
         ->firstOrFail(); 

     $ids_to_delete = $messages->pluck('id'); 

     Outbox::destroy($ids_to_delete); 

     return view('messages') 
       ->with('messages', $messages);; 
    } 
} 

步骤3 - Create a view

资源/视图/ messages.blade.php

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title> 
      laravel.com 
     </title> 
    </head> 
    <body> 
    <div> 
     <messages> 
     @foreach($messages as $message) 
      <message msisdn="{{ $message->rcpt }}">{{ $message->body }}</message> 
     @endforeach 
    </div> 
    </body> 
</html> 

步骤4 - Add a route for your new controller

应用程序/路由/ web.php

Route::get('messages/{sender}', '[email protected]'); 

第5步 - 访问新的UR大号

http://localhost/messages/device

设备是什么$_GET['device']本来。