我正在使用这个脚本,见下文,我从这个站点获得了登录用户并根据他们的登录重定向到网页。它工作得很好,但我无法将用户名拉到他们重定向到的页面中。下面的新会话脚本通常适用于简单脚本,但似乎不适用于此重定向脚本。如果有人能指出我正确的方向,我将不胜感激,因为我对 PHP 很陌生,这是我的第一次尝试。谢谢艾伦
重定向脚本效果很好。
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="trydata"; // Database name
$tbl_name="users"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// 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);
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$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['username'] = $myusername;
$_SESSION['password'] = $mypassword;
$result = mysql_fetch_array($result); // get the result set from the query
$redirect = trim($result['redirect']); // get the redirect column's value
if ($redirect == '') {
echo "No redirect value was set!";
} else {
header('Location: ' . $redirect);
exit;
}
} else {
echo "Wrong Username or Password";
}
?>
New session script not pulling loin information through.
<?php
session_start(); // start up your PHP session!
if (isset($_SESSION['username'])
{
echo "You are logged in as ".$_SESSION['username'];
echo "<p>";
echo "<a href='logout.php'> Click here to logout<a/>";
}
else
header ("Location: form.php");
?>