2014-02-12 111 views
-1

我在我的網站上註冊會話時遇到了問題登錄腳本:我嘗試了以下腳本,但它一直重定向到主登錄頁面,我懷疑腳本沒有註冊session.please幫助我。 這裏是腳本會話註冊不起作用

checklogin.php

<?php 
$con=mysqli_connect("localhost","root","p11","daot"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql=mysqli_query($con,"SELECT * FROM registration WHERE 
Username='$myusername' and Password='$mypassword'"); 


// Mysql_num_row is counting table row 
$count=mysqli_num_rows($sql); 

// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 
session_start(); 
$_SESSION["myusername"]; 
$_SESSION["mypassword"]; 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 

login_success.php

<?php 
session_start(); 
if (!isset($_SESSION['myusername'])) 
    { 
    header('Location:login.php'); 
    } 
    ?> 

<html> 
<body> 
Login Successful 
</body> 
</html> 
+0

一如既往:'session_start();'必須在頁面頂部。 –

+0

我把它放在頂部,但沒有任何改變。 – user3279301

+0

@@ john Conde希望....不是強制性的放置頂部/第一,,,會話必須開始之前去操縱 – user1844933

回答

1

與此更換checking.php和嘗試PLS:

<?php 
session_start(); 

$con=mysqli_connect("localhost","root","p11","daot"); 

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql=mysqli_query($con,"SELECT * FROM registration WHERE 
Username='$myusername' and Password='$mypassword'"); 


// Mysql_num_row is counting table row 
$count=mysqli_num_rows($sql); 

// If result matched $myusername and $mypassword, table row must be 1 row 

if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 

$_SESSION["myusername"] = $mypassword; 
$_SESSION["mypassword"] = $mypassword; 
header("location:login_success.php"); 
} 
else { 
echo "Wrong Username or Password"; 
} 
?> 
+0

謝謝你的快速反應,真的有用! – user3279301

+0

yw;),你可以正確回答 – ImadOS

0

將您的session_start()移動到頁面頂部。 此外,您沒有分配任何值 $ _SESSION [「myusername」];或$ _SESSION [「mypassword」]; 在初始化$ myusername和$ mypassword之後設置這些值。

$_SESSION["myusername"] = $myusername; 
$_SESSION["mypassword"] = $mypassword; 
+0

就像我做過的人一樣。在你4分鐘之前 – ImadOS

+0

謝謝,它的工作原理! – user3279301

+0

@ImadOS不投票,除非它是錯誤的答案...... – user1844933