2013-12-20 31 views
-1

好的,SO。經過五個多小時的篩選,找出潛在的重複項,並將可能的解決方案應用到我的項目中,甚至下載一個PHP IDE以確保我的語法對每個人都很好和整潔。我終於到了需要一些建議的地步。當提供正確的登錄憑證時,會話無法啓動

我的兩個問題(這可能與):

當有人登錄,成功地與測試參數我已經存儲在數據庫中,他們是不定向(也許我的if語句是不正確的?)

當頁面加載沒有第一次嘗試時,我的「錯誤密碼 - 用戶名組合」消息正在顯示。我相當肯定爲爲什麼但不是太確定如何解決它

<?php session_start(); // this line of code has been added by the instruction of a comment. 
    if(isset($submit)) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    } 
$con = mysqli_connect("***","***","***","***"); 
$S_username = mysqli_real_escape_string($con, $username); 
$S_password = mysqli_real_escape_string($con, $password); 

if(mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$sql = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$S_username' AND `password` = '$S_password'"); 

if(!$sql) { 
die(mysqli_error($con)) ; 
} 

$check_again = mysqli_num_rows($sql); 

    if($check_again == 1) { 
    session_start(); // this line of code has been deleted 
    $_SESSION['logged in'] = TRUE; 
    $_SESSION['username'] = $S_username; 
    header("Location: http://terrythetutor.com/submitvideo.php"); 
    } 
    else { 
    echo "Your username and password combination was not recognised. Please try again." ; 
    } 

?> 
<html> 
<head> 
<title>Login Page</title> 
<link rel="stylesheet" type="text/css" href="style.css"> 
</head> 
    <?php include_once 'googleanalytics.php'; ?> 
<body> 
<a href="http://terrythetutor.com"> 
    <div class="banner"> </div> 
</a> 
<?php include 'menu.php'; ?> 
<h1 align="center">Please login to access restricted files</h1> 
</br> 
</br> 
</br> 
</br> 
<div align="center"> 
<form action = "login.php" method = "post"> 

Username: <input type = "text" name = "username"></br></br> 

Password: <input type = "password" name = "password"></br></br> 

<input type = "submit" value = "Login" name="submit"> 

</form> 
</div> 
</body> 

</html> 

任何和所有的反饋是歡迎的。謝謝。

+1

嘗試把'在session_start();'爲您打開'

+0

@ Fred-ii-我加了'session_start();'但不幸的是,它沒有改變任何東西。我應該從if語句中刪除session_start嗎? – user2666324

+0

是的,只有一個,如果你打算把它放在上面。它不能在其他地方(除非你有不同的條件語句)。另外,如果你使用其他文件,他們也需要它。 I.e .:'includes'和'menus'等 –

回答

0

使用session_start();只有一次,在頂部..

+0

謝謝,我剛剛通過Fred-ii-的指示改變了這一點。但是,我注意到,當我輸入登錄憑據時,它甚至不會重定向到其他頁面。任何想法爲什麼? – user2666324

+0

使用這篇文章[鏈接](http://www.phpeasystep.com/phptu/6.html)或這篇文章[鏈接](http://php.about.com/od/finishedphp1/ss/php_login_code_2.htm ) –

相關問題