2014-12-03 26 views
0

我想爲使用Heroku的網站製作基本的帳戶註冊頁面。我已經安裝了PostGRESQL,並創建了一個名爲users的表格,其中包含所有正確的列。 (我還沒有使用加密但因爲我只是試圖讓這個工作的第一位。)這是我的頁面PHP:使用Heroku製作註冊頁面 - NotFoundException錯誤

<?php 
require('../vendor/autoload.php'); 
require('../includes/config.php'); 
$app = new Silex\Application(); 
$app['debug'] = true; 
// Register the monolog logging service 
$app->register(new Silex\Provider\MonologServiceProvider(), array(
'monolog.logfile' => 'php://stderr', 
)); 
// Register the Twig templating engine 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../views', 
)); 

/*checking database*/ 
    // if form was submitted 
    if ($_SERVER["REQUEST_METHOD"] == "POST") 
    { 
     /**/ 
     //check that all three fields have been filled out 
     if (empty($_POST["username"])||empty($_POST["password"])||empty($_POST["confirmation"]))   
     {  
      apologize("You need to complete the username, password, and confirmation fields."); 
      exit; 
     }  
     //check that password and confirmation are the same 
     if ($_POST["password"] != $_POST["confirmation"]) 
     { 
      apologize("Password and confirmation must match."); 
      exit; 
     } 

     if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) 
     { 
      apologize("Email address not valid. Try again.");   
      exit ; 
     }  


// Register the Postgres database add-on 
$dbopts = parse_url(getenv('DATABASE_URL')); 
$app->register(new Herrera\Pdo\PdoServiceProvider(), 
array (
    'pdo.dsn' => 'pgsql:dbname='.ltrim($dbopts["path"],'/').';host='.$dbopts["host"], 
    'pdo.port' => $dbopts["port"], 
    'pdo.username' => $dbopts["user"], 
    'pdo.password' => $dbopts["pass"] 
    ) 
     ); 

$st = $app['pdo']->prepare('INSERT INTO users (username, email, hash) VALUES ('. $_POST["username"] . ', ' . $_POST["email"]. ', '. $_POST["password"]. ')'); 
$st->execute();  

$app->get('/db/', function() use($app) { 

$st = $app['pdo']->prepare('SELECT username FROM users'); 
$st->execute(); 
$names = array(); 
while ($row = $st->fetch(PDO::FETCH_ASSOC)) { 
$app['monolog']->addDebug('Row ' . $row['name']); 
$names[] = $row; 
} 
return $app['twig']->render('database.twig', array(
'names' => $names 
)); 
}); 
$app->get('/twig/{name}', function($name) use($app) { 
return $app['twig']->render('index.twig', array(
'name' => $name, 
)); 
}); 
$app->run(); 
     //if the registration worked, log the user in 
     if ($result !== false) 
     { 
      //if registration worked, remember that session ID 
      $rows = query("SELECT LAST_INSERT_ID() AS id"); 
      $id = $rows[0]["id"]; 

      $_SESSION["id"] = $id; 
      redirect("../index.php"); 
     } 
    } 
    else 
    { 
     // else render form 
     render("register_form.php", ["title" => "Register"]); 
    } 

?> 

我收到以下錯誤,當我提交頁面:

對不起,您正在尋找的頁面找不到。 2/2 NotFoundHttpException在RouterListener.php管線145:未發現 「POST /」 路線(從 「http://secret-ridge-6332.herokuapp.com/register.php」)

in RouterListener.php line 145 
at RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) 
at call_user_func(array(object(RouterListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) in EventDispatcher.php line 164 
at EventDispatcher->doDispatch(array(array(object(RouterListener), 'onKernelRequest'), array(object(LocaleListener), 'onKernelRequest'), array(object(LogListener), 'onKernelRequest'), array(object(MiddlewareListener), 'onKernelRequest')), 'kernel.request', object(GetResponseEvent)) in EventDispatcher.php line 53 
at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in HttpKernel.php line 126 
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 66 
at HttpKernel->handle(object(Request), '1', true) in Application.php line 538 
at Application->handle(object(Request)) in Application.php line 515 
at Application->run() in register.php line 72 

1/2 ResourceNotFoundException在UrlMatcher.php線96:

in UrlMatcher.php line 96 
at UrlMatcher->match('/') in RedirectableUrlMatcher.php line 30 
at RedirectableUrlMatcher->match('/') in LazyUrlMatcher.php line 51 
at LazyUrlMatcher->match('/') in RouterListener.php line 127 
at RouterListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) 
at call_user_func(array(object(RouterListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) in EventDispatcher.php line 164 
at EventDispatcher->doDispatch(array(array(object(RouterListener), 'onKernelRequest'), array(object(LocaleListener), 'onKernelRequest'), array(object(LogListener), 'onKernelRequest'), array(object(MiddlewareListener), 'onKernelRequest')), 'kernel.request', object(GetResponseEvent)) in EventDispatcher.php line 53 
at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in HttpKernel.php line 126 
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 66 
at HttpKernel->handle(object(Request), '1', true) in Application.php line 538 
at Application->handle(object(Request)) in Application.php line 515 
at Application->run() in register.php line 72 

任何想法我可能做錯了什麼?謝謝!

回答

1

是的,你正在使用Silex混合你的舊編程方式。

一般來說,你可以用$app->get('/someroute', function(Request $request) { ... });來響應GET請求,你的代碼爲該函數內部的「/ someroute」,POST請求爲$app->post()。您不以任何方式使用$_GET,$_POST等超全局變量,而是使用傳遞給這些控制器函數的請求對象來訪問請求數據。

請閱讀一些Silex教程和主要的Silex手冊以瞭解更多信息。

我試着清理一下你的應用程序,並給它一個索引頁,一個登錄表單路由和POST處理程序,以及一個註冊表單路由和POST處理程序。請使用帶有綁定參數的準備好的語句來保護自己免受SQL注入攻擊,並且請勿將密碼以純文本形式存儲在數據庫中,但使用BCrypt的crypt()正確。

<?php 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 

require __DIR__.'/../vendor/autoload.php'; 

$app = new Silex\Application(); 

$app->register(new Silex\Provider\MonologServiceProvider(), [ 
    'monolog.logfile' => 'php://stderr', 
]); 
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/../views', 
)); 
$dbopts = parse_url(getenv('DATABASE_URL')); 
$app->register(new Herrera\Pdo\PdoServiceProvider(), array(
    'pdo.dsn' => sprintf('pgsql:dbname=%s;host=%s;port=%s', ltrim($dbopts["path"],'/'), $dbopts["host"], $dbopts["port"]); 
    'pdo.username' => $dbopts["user"], 
    'pdo.password' => $dbopts["pass"], 
    'pdo.options' => array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION), 
)); 

$app->get('/', function(Request $request) use($app) { 
    // your index code goes here, maybe render index.html.twig: 
    return $app['twig']->render('index.html.twig'); 
}); 

$app->get('/login', function(Request $request) use($app) { 
    // render login form here 
    return $app['twig']->render('login.html.twig'); 
}); 

$app->post('/login', function(Request $request) use($app) { 
    $email = $request->request->get('email'); 
    $password = $request->request->get('password'); 
    // try to log in here 
    $s = $app['pdo']->prepare('SELECT * FROM users WHERE email = :email'); 
    if($s->execute(array(':email' => $email))) { 
     $u = $s->fetch(); 
     // compare crypted password to stored hash, constant time and all 
     // the hash acts as the salt at the same time, clever stuff 
     if(!password_verify($password, $u['password'])) { 
      // password wrong 
     } else { 
      // login okay 
     } 
    } else { 
     // no such user 
    } 
}); 

$app->get('/register', function(Request $request) use($app) { 
    // render registration form here 
    return $app['twig']->render('register.html.twig'); 
}); 

$app->post('/register', function(Request $request) use($app) { 
    $email = $request->request->get('email'); 
    // hash password using bcrypt (safe "2y" algorithm, cost factor 10, random salt) 
    $password = password_hash($request->request->get('password'), PASSWORD_BCRYPT); 
    $s = $app['pdo']->prepare('INSERT INTO users (email, password) VALUES(:email, :password)'); 
    $s->execute(array(':email' => $email, ':password' => $password)); 
    // success page unless there is an exception 
}); 

我沒有測試這個,但你應該明白了。

+0

非常感謝您花時間回覆。我今天需要一些時間來完成這個工作,絕對是Silex手冊。 – garson 2014-12-05 16:07:04

+0

心靈接受答案然後,@ garson? – dzuelke 2014-12-06 21:14:04