【发布时间】:2012-07-01 21:43:00
【问题描述】:
当我运行此脚本时,我试图通过标头将会话重定向到 location:index.php 来回显会话变量($email),但它只是回显会话值(数字 1)而不是电子邮件地址我不知道为什么。有任何想法吗?谢谢。
<?php
session_start();
if (isset($_POST['email'], $_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
if ($email && $password) {
$connect = mysql_connect("localhost", "root", "") or die("Could not connect");
mysql_select_db("test") or die("could not find database");
$query = mysql_query("SELECT * FROM users WHERE email='$email'&&
password= md5('$password')");
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
$_SESSION["email"] = $email;
session_write_close();
header("location:index.php");
Die();
}
else
echo ("not a user");
}
else
echo("enter both fields");
}
?>
<html>
<form action='edit1.php' method='POST'>
<table align='right' bgcolor='blue'>
<tr>
<td>Email
</td>
<td>Password
</td>
</tr>
<tr>
<td><input name='email' type='text'>
</td>
<td><input name='password' type='password'><td>
<input type='submit' value='login'>
</td>
</tr>
</table>
</form>
</html>
//index.php file
<?php
session_start(); {
echo $_SESSION['email'] or die('fail'); {
}
}
?>
【问题讨论】:
-
你确定 $email 不是 1 吗?
-
不,虽然它是第一个字符
-
更改 echo $_SESSION['email'] 或死('失败');回显 $_SESSION['email'];
-
就是这样,非常感谢你,我失去了生活的意愿,无法相信这有多简单,感谢你,你的明星:D
-
另外,您很容易受到 sql 注入 (php.net/manual/en/security.database.sql-injection.php) 的影响。请使用 mysql_real_escape_string,或者更好的是,使用准备好的语句更改为 PDO
标签: php session session-variables