你好,我对pdo(php)比较陌生,我创建了一个基本的登录系统,我知道它还不安全,但我只是在试验,无论如何,我知道在旧的php中,你可以在if语句中回声和错误信息,但它似乎不再工作了,这里是我的脚本,我做错了什么,或者你只是不能在pdo中这样做了。
if ($row == null){
header( "location: login.html");
echo "login failed";
} else{
header("location: homepage.php");
}
我意识到这可能没有足够的代码可用,所以这里是脚本的其余部分
session_start();
//connection String
$connection = new PDO("sqlsrv:server=server;Database=database", "username", "password");
//Seelcting function
$smt = $connection->prepare("select user_id, username from account where username = :username and password =:password");
//setting values to textboxes
$username = $_POST["txt_username"];
$password = $_POST["txt_password"];
//binding values
$smt->bindValue(':username', $username);
$smt->bindValue(':password', $password);
//execution
$smt->execute();
//fetching data
$row = $smt->fetch( PDO::FETCH_ASSOC ) ;
echo "$row[user_id]'n'n";
echo "$row[username]'n'n";
$_SESSION{"user_id"} = $row["user_id"];
发送
header( "location: login.html");
浏览器将重定向到新文件(login.html
),并忽略(几乎)任何进一步的输出。
如果稍后在login.html
上显示消息,则必须使用某种机制将消息传输到该页面,例如,通过使用会话变量。
编辑
header
命令在实际内容之前发送一些数据给浏览器。如果使用标头让浏览器重定向,用户将永远无法看到内容。
所以你需要某种方式将内容带到下一页,你正在重定向到。
一种可能是使用会话变量。
if ($row == null){
$_SESSION['errormsg'] = "login failed";
header( "location: login.php");
} else{
header("location: homepage.php");
}
在login.php
中,如果存在此消息,则可以对其作出反应:
if( isset( $_SESSION['errormsg'] ) ) {
// do the output
echo $_SESSION['errormsg'];
// delete the message from the session, so that we show it only once
unset( $_SESSION['errormsg'] );
}