我有一个 checklogin.php 脚本,它可以很好地将用户重定向到成功登录的特定页面。我现在想将其设置为重定向到将用户重定向到登录表单的原始 index.php 页面。在 index.php 的顶部,我包括:
<?php
session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
我已检查 $_SESSION['url'] 在此页面上的设置是否正确。
main_login.php 只包含由 checklogin.php 处理的登录表单:
<form name="form1" method="post" action="checklogin.php">
并且 $_SESSION['url'] 在此页面上也已正确设置。
checklogin.php 看起来像这样:
<?php
session_start();
print_r($_SESSION['url']);
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$encrypted_mypassword=md5($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:$_SESSION['url']");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
如您所见,我试图在此脚本的顶部打印 $_SESSION['url'] 但没有返回任何内容。
有人可以帮忙吗?
谢谢,
缺口