你好,我是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"];发布于 2013-05-30 19:43:52
在您发送了
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'] );
}https://stackoverflow.com/questions/16835124
复制相似问题