2015-07-19 52 views
0

IM使用一個登錄頁面:當我更改頁面時,如何讓我的用戶登錄? PHP

<?php 
session_start(); 
include_once 'dbconnect.php'; 

if(isset($_SESSION['user'])!="") 
{ 
    header("Location: home.php"); 
} 

if(isset($_POST['btn-login'])) 
{ 
    $email = mysql_real_escape_string($_POST['email']); 
    $upass = mysql_real_escape_string($_POST['pass']); 
    $res=mysql_query("SELECT * FROM users WHERE email='$email'"); 
    $row=mysql_fetch_array($res); 

    if($row['password']==md5($upass)) 
    { 
     $_SESSION['user'] = $row['user_id']; 
     header("Location: home.php"); 
    } 
    else 
    { 
     ?> 
     <script>alert('wrong details');</script> 
     <?php 
    } 

} 
?> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>ورود</title> 
<link rel="stylesheet" href="style.css" type="text/css" /> 
</head> 
<body> 
<center> 
<div id="login-form"> 
<form method="post"> 
<table align="center" width="30%" border="0"> 
<tr> 
<td><input type="text" name="email" placeholder="Your Email" required /></td> 
</tr> 
<tr> 
<td><input type="password" name="pass" placeholder="Your Password" required /></td> 
</tr> 
<tr> 
<td><button type="submit" name="btn-login">ورود</button></td> 
</tr> 
<tr> 
<td><a href="register.php">ثبت نام</a></td> 
</tr> 
</table> 
</form> 
</div> 
</center> 
</body> 
</html> 

,如果我輸入正確的用戶名和密碼,我去歡迎頁面,但登錄只是在該網頁IM,當我去到另一個網頁IM像一個客人用戶再次。

我該如何解決它,並保持登錄在每一個地方?

+0

在每個頁面中用戶'session_start()'! –

+0

確保您已在其他頁面的頂部添加* session_start(); *。如果您將通用函數(如db連接,session_start等)添加到單獨的頁面並將其包含在任何需要的地方,這將是一個好習慣。 –

+0

你在哪裏檢查是否設置了$ _SESSION ['user']'並從db中加載用戶?當你記錄用戶頭部重定向時,顯式調用'session_write_close'也不會傷害到它。 – Rufinus

回答

0

在每個頁面頂部啓動會話將解決您的問題。

然後檢查$ _SESSION ['user']。如果其當前顯示登錄用戶,則重定向到登錄頁面。