2014-09-28 91 views
0

當我直接進入索引頁面時,會將我引導至登錄頁面。當我輸入電子郵件和密碼並提交時,它會保留在登錄頁面上。會話停留在登錄頁面

我認爲問題出在配置文件和索引頁面之間的會話中。

下面是配置:(請不要集中到MySQL,我仍然想使用它)

<?php 
ob_start(); 
error_reporting(E_ALL^E_NOTICE); 
ini_set("display_errors", true); 
error_reporting(-1); 
ini_set('display_errors', 'On'); 
mysql_connect("","","") or die("cannot connect"); 
mysql_select_db("") or die("Gagal"); 
$myemail= $_POST['myemail']; 
$mypassword= $_POST['mypassword']; 
$sql= "SELECT * FROM user WHERE myemail='".$myemail."' and mypassword='".$mypassword."'"; 
$result=mysql_query($sql); 
$count=mysql_num_rows($result); 
if($count==1) 
    { 
    echo "Login successful"; 
    session_register("myemail"); 
    session_register("mypassword"); 
    header("location:index.php"); 
    } 
else { 
    echo "Wrong Username or Password"; 
    } 
ob_end_flush(); 
?> 

然後在索引頁我有這樣的會議標題:

<?php 
session_start(); 
if(!session_is_resgitered(myemail)){ 
header("location:login.html); 
} 
?> 

請幫我清除這個,我已經嘗試了很多方法來實現這個登錄功能。謝謝。

+0

should't這個'如果(!session_is_resgitered(myemail)){'是'如果(!session_is_resgitered( 「myemail」)){'我指的是「在myemail附近,我不確定,你可能也需要'login_start()'在login.php? – Yazan 2014-09-28 12:15:51

+0

不,它不能解決問題。 – 2014-09-28 12:18:54

+0

你是否在session.php?中添加了session_start() line) – Yazan 2014-09-28 12:19:29

回答

0

登錄proccess文件

<?php 
session_start(); 
ob_start(); 

mysql_connect("localhost","root","") or die("cannot connect"); 
mysql_select_db("yourdatabase") or die("Gagal"); 

$myemail = $_POST['myemail']; 
$mypassword = $_POST['mypassword']; 

$sql = mysql_query("SELECT * FROM user WHERE email = '{$email}' AND password = '{$password}'"); 
$count = mysql_num_rows($sql); 
if ($count == 1) { 
    $_SESSION['email'] = $myemail; 
    $_SESSION['password'] = $mypassword; 

    header("location: index.php"); 
} else { 
    echo "Wrong Username or Password"; 
} 
ob_end_flush(); 
?> 

您的索引文件

<?php 
session_start(); 

echo $_SESSION['email']; // try this first, if you make it right from the login page then it will give a value 

// if ($_SESSION['email'] == null && $_SESSION['password'] == null) { 
// header("location:login.html"); 
// } 
?> 
+0

它仍然不能解決它。它仍然指引我回到登錄頁面。 – 2014-09-29 02:52:55

+0

我被編輯了答案嘗試索引文件 – 2014-09-29 07:35:26

+0

如果你想讓腳本正確重定向,你必須在'header('Location:index.php')後面使用'exit;';'' – DanFromGermany 2014-09-29 07:53:45

0

在配置PHP:你可以這樣做。

if($count==1) 
    { 
    echo "Login successful"; 
    $_SESSION['user_loggedin']=$yourEmail; // this will create a session variable 
    //session_register("myemail"); 
    //session_register("mypassword"); 
    header("location:index.php"); 
    } 

和index.php文件

<?php 
session_start(); 
$checkUser= $_SESSION['user_loggedin']; //here you can access the logged in user if it is logged in 
//check the user 
if(strlen($checkUser)){ 
    // user is logged in and access other details for current user 
}else{ 
    // user is not logged in 
} 

?>