我有一个小问题。我有一个登录框与提交按钮,但我不能重定向到另一个页面。我插入电子邮件和密码,但它不工作,它只是停留在第一页。我能做什么?
我用这个:
header("Location: home.php");
下面是我的代码:
<?php session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user'])!="") {
header("Location: home.php");
}
if(isset($_POST['btn-login'])) {
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM users WHERE email='$email'");
$row=mysql_fetch_array($res);
if($row['password']==md5($upass)) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {
?>
<script>alert('wrong details');</script>
<?php } } ?>
首先isset($_SESSION['user'])
返回布尔值:true
或false
而不是",因此必须使用if(isset($_SESSION['user']) && $_SESSION['user']!="")
。
第二,你所有的mysql_*
功能都被弃用了。
<?php session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user']) && $_SESSION['user']!='') {
header("Location: home.php");
}
if(isset($_POST['btn-login'])) {
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM users WHERE email='$email'");
$row=mysql_fetch_array($res);
$_SESSION['user']='we have pw';
if( $row['password']==md5($upass) ) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {?>
<script>alert('wrong details');</script>
你的密码是否被转换为md5 ??这一行应该是问题所在。注意:如果这是你的home.php,那么它会重定向一整天
修改
header("Location: home.php");
echo "<script>window.location.assign("home.php")</script>";
试试这个:
<?php session_start();
include_once 'dbconnect.php';
if(isset($_SESSION['user'])!="") {
header("Location: home.php");
}
if(isset($_POST['btn-login'])) {
$email = mysql_real_escape_string($_POST['email']);
$upass = mysql_real_escape_string($_POST['pass']);
$res=mysql_query("SELECT * FROM users WHERE email='$email' AND password= 'md5($upass)' ");
$row=mysql_fetch_array($res);
if($row) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {
?>
<script>alert('wrong details');</script>
<?php }} ?>