2017-05-31 17 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']本來。