2015-07-28 105 views
-1
session_start(); 

if(isset($_POST['submit'])) { 

$uname = $_POST['uname']; 

$pw = $_POST['pw']; 

    require_once('db.php'); 

    $sql = 'SELECT * FROM users_table 
       WHERE username="'.mysql_escape_string($uname).'" AND password="'.mysql_escape_string(md5($pw)).'" 
      LIMIT 0, 1 
    '; 
    $qry = mysql_query($sql); 
    $count = mysql_num_rows($qry); 

    if($count > 0) { 
     $_SESSION['username'] = $uname; 
     $_SESSION['password'] = $pw; 
     header('Location: products_list.php'); 
    } else { 
     header('Location: index.php?error=1'); 
    } 
} 

回答

1

使用的setcookie()函数来设置cookie,然后检索它,当用户接取我已经使用cookie来实现它的登录限制的页面

setcookie description

0

,它运行惊人完美... 只有你需要做的只是在cookie中添加编码以保证安全......

session_start(); 

if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['submit'])) { 

$uname = $_POST['uname']; 

$pw = $_POST['pw']; 

require_once('db.php'); 

//Checking whether the cookies are set or not 
if(!empty($_COOKIES['Last_Login_UserID']) && !empty($_COOKIES['Last_Login_Password'])){ 
    if($_COOKIES['Last_Login_UserID']==$uname && $_COOKIES['Last_Login_Password']==$pw){ 
     //Cookies are perfect give access 
     $_SESSION['username'] = $uname; 
     $_SESSION['password'] = $pw; 
     header('Location: products_list.php'); 
    }else{ 
     //Cookies cookies are wrong 
     login_check($uname,$pw); 
    } 

}else{ 
    //Cookies are not set so check the database 
    login_check($uname,$pw); 
} 
//Function to check the login 
function login_check($uname,$pw){ 
    $sql = 'SELECT * FROM users_table WHERE username="'.mysql_escape_string($uname).'" AND password="'.mysql_escape_string(md5($pw)).'" LIMIT 0, 1 ;'; 
$qry = mysql_query($sql); 
$count = mysql_num_rows($qry); 

    if($count == 1) { 
     $_SESSION['username'] = $uname; 
     $_SESSION['password'] = $pw; 
     if(!empty($_POST['remember_me']) && $_POST['remember_me']==true){ 
      setcookie('Last_Login_UserID',$_SESSION['username'],(60*60*24),"/"); 
      setcookie('Last_Login_Password',$_SESSION['password'],(60*60*24),"/"); 
     } 
     header('Location: products_list.php'); 
    } else { 
     header('Location: index.php?error=1'); 
    } 
}} 
+1

我是新来的PHP其实你是怎么想出来的? ($ _COOKIES ['Last_Login_UserID'])&&!empty($ _ COOKIES ['Last_Login_Password'] –

+0

刚刚在函数login_check中看到,如果remember_me被设置且值为true,代码设置cookie并记住用户 –

+0

我复制并粘贴你的代码和我没有被导向到products_list.php文件 –