什么是在会话结束后让用户登录的最佳方式?

时间:2015-02-11 01:54:55

标签: php cookies

我正在为一个类的简单登录页面工作,并计划在关闭浏览器后使用cookie来保持用户登录(如果他们选择)。我使用复选框输入按钮作为设置cookie的大小写。用户进入登录页面并登录后,我将其发送到脚本以检查有效的用户名和密码,我还检查是否使用了该按钮

  #QotD.php

  if(isset($_GET['signed_in']))      #check box value
  if($_GET['signed_in']=="on"){ 
  if(isset($_GET['username']))
    $username = $_GET['username'];
    setcookie('username',$username,time()+10000);#cookie set with username
  }

我想要做的是在登录页面文件的开头有一个条件语句,检查是否设置了cookie以及它是否直接进入主页面。

#QotD_homepage.php
if(isset($_COOKIE['username'])){
header("Location: main_page.php");
exit();
}

问题是它似乎让用户签名是否关闭该框。我尝试添加一个按钮来取消设置cookie,但它没有用。是否有更有效的方式以这种方式处理cookie?

2 个答案:

答案 0 :(得分:2)

首先,要登录用户,您将需要使用POST操作方法,因为它会隐藏来自网址的信息。 GET方法包含网址中的信息,可以轻松复制和入侵。

其次,如果陈述应该是这样的话

if(isset($_GET['username']))
{ 
  $username = $_GET['username'];
  # do something with username...
  if(isset($_GET['signed_in']) && $_GET['signed_in']=="on")
    setcookie('username',$username,time()+10000);
  }
}

要解决有关用户每次登录原因的问题,即使您没有设置Cookie,原因可能是因为您没有unset Cookie。这通常是通过logout页面完成的。

使用以下代码创建一个注销页面:

setcookie('username', null, 1);

然后每次您想要取消设置cookie以测试登录时运行此页面而不勾选复选框。

希望有所帮助:)

答案 1 :(得分:1)

如果条件语句错误。使用end if或者使用{}括号结束它。使用以下代码

<?php
  if(isset($_GET['signed_in'])) {     #check box value
  if($_GET['signed_in']=="on"){ 
  if(isset($_GET['username']))
    $username = $_GET['username'];
    setcookie('username',$username,time()+10000);#cookie set with username
  }
}
?>

OR

<?php
  if(isset($_GET['signed_in'])) :     #check box value
  if($_GET['signed_in']=="on"){ 
  if(isset($_GET['username']))
    $username = $_GET['username'];
    setcookie('username',$username,time()+10000);#cookie set with username
  }
endif;
?>

希望这有助于你