我尝试将用户重定向到他们的个人资料,其标题中包含用户名。但是,重定向不会显示用户名。登录页面要求提供电子邮件和密码(省略用户名)。有人能告诉我我做错了吗?
在class.php中:
public function register($uname,$email,$upass,$code)
{
try
{
$password = md5($upass);
$stmt = $this->conn->prepare("INSERT INTO tbl_users(userName,userEmail,userPass,tokenCode)
VALUES(:user_name, :user_mail,
:user_pass, :active_code)");
$stmt->bindparam(":user_name",$uname);
$stmt->bindparam(":user_mail",$email);
$stmt->bindparam(":user_pass",$password);
$stmt->bindparam(":active_code",$code);
$stmt->execute();
return $stmt;
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
public function login($email,$upass,$uname)
{
try
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_users WHERE
userEmail=:email_id AND userName=:user_name");
$stmt->execute(array(":email_id"=>$email,":user_name"=>$uname));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() == 1)
{
if($userRow['userStatus']=="Y")
{
if($userRow['userPass']==md5($upass))
{
$_SESSION['userSession'] = $userRow['userID'];
$_SESSION['userName'] = $userRow['userName'];
return true;
重定向代码:
public function redirectchannel()
{
header("Location: index.php?id=$userName");
}
登录页面:
if(isset($_POST['btn-login']))
{
$email = trim($_POST['txtemail']);
$upass = trim($_POST['txtupass']);
if($user_login->login($email,$upass))
{
$user_login->redirectchannel();
}
}
<input type="email" class="reginput" placeholder="Email address" name="txtemail" required />
<input type="password" class="reginput" placeholder="Password" name="txtupass" required />
<button class="formbutton" type="submit" name="btn-login">Login</button>
登陆页面:
$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
答案 0 :(得分:0)
虽然我不知道为什么您将用户名放在查询字符串中,因为您已经在会话中使用了该用户名,但仍然:
public function redirectchannel()
{
// what is `$userName` here? It is NULL
header("Location: index.php?id=$userName");
}
所以,如果您仍想将用户名作为查询参数传递,我想您可以这样做:
public function redirectchannel()
{
header("Location: index.php?id=" . $_SESSION['userName']);
exit; //always exit immediately after location headers.
}
答案 1 :(得分:0)
如果我正确理解了这一点,您就不需要通过网址传递用户名值。您已经使用用户名创建了一个会话变量:
$_SESSION['userName']
您可以在使用相同会话的任何页面中使用此相同变量。只需创建一个变量,如:
$username = $_SESSION['userName'];
现在您可以随时在页面上调用此变量,如下所示:
User Name: <?php echo $username; ?>