<?php
session_start();
error_reporting(E_ALL ^ E_DEPRECATED);
$host = "localhost";
$user = "root";
$pass = "";
$db = "testing";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
if (isset($_POST['loginID'])) {
$loginID = $_POST['loginID'];
$password = $_POST['password'];
$sql = "SELECT * FROM user WHERE loginID='".$loginID."' AND password='".$password."' LIMIT 1";
$res = mysql_query($sql);
if (mysql_num_rows($res) == 1) {
header("Location:homepage.php");
$_SESSION['loginID'] = $loginID;
} else {
header("Location:login.php");die;
}
}
?>
我想在会话中传递用户名,以便在homepage.php
中显示用户名,但是当我尝试这样做时它没有用。有什么问题,我怎样才能让它发挥作用?
答案 0 :(得分:1)
改变这个:
if(mysql_num_rows($res)==1)
{
header("Location:homepage.php");
$_SESSION['loginID']=$loginID;
}
到此:
if(mysql_num_rows($res)==1)
{
$_SESSION['loginID']=$loginID;
header("Location:homepage.php");
}
答案 1 :(得分:0)
会话值,然后重定向到另一个页面。试试以下: 如果(mysql_num_rows($水库)== 1) { $ _SESSION [ '登录ID'] = $登录ID; 标题( “位置:homepage.php”); }
答案 2 :(得分:0)
您需要从结果集中获取一行,并使用该行的信息作为会话变量:
$res = mysql_query($sql);
if ($row = mysql_fetch_assoc($res)) {
$_SESSION['loginID'] = $loginID;
$_SESSION['name'] = $row['username']; // or whatever the column is called where the username is stored
header("Location:homepage.php");
exit();
} else {
header("Location:login.php");die;
}
除了一些评论:
mysql_*
函数,你现在有一个SQL注入问题;