2015-07-13 174 views
2

我試圖讓我的網頁登錄之後刷新是我的index.php登錄後刷新頁面?

<?php 
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) 
{ 
    // let the user access the main page 
    // i have made the main content with jquery mobile framework 
} 
elseif(!empty($_POST['username']) && !empty($_POST['password'])) 
{ 
    // let the user login 
     include "login.php"; 

} 
else 
{ 
    // display the login form 
<form method="post" action="index.php" name="loginform" id="loginform"> 
      <fieldset> 
       <label for="username">Username:</label><input type="text" name="username" id="username" /><br /> 
       <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> 
       <input type="submit" name="login" id="login" value="Login" /> 
      </fieldset> 
      </form> 

的裏面我的login.php格式

<?php 

    $username = mysql_real_escape_string($_POST['username']); 
    $password = mysql_real_escape_string($_POST['password']); 

    $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'"); 

    if(mysql_num_rows($checklogin) == 1) 
    { 
     $row = mysql_fetch_array($checklogin); 
     $email = $row['email']; 
     $uid = $row['u_id']; 

     $_SESSION['Username'] = $username; 
     $_SESSION['EmailAddress'] = $email; 
     $_SESSION['LoggedIn'] = 1; 
     $_SESSION['uid'] = $uid; 
     $flag=1; 

    header("Location: http://5aday.dihops.net/index.php"); 
//  echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">"; 
//header("Refresh: 2; url=http://5aday.dihops.net/index.php"); 
    // THIS IS WHERE I AM STUCK 
     } 
    else 
    { 
     echo "<h1>Error</h1>"; 
     echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>"; 
    } 
?> 
    } 
    ?> 

我不知道我做錯了。但是,一旦用戶提供了他的用戶名和密碼並提交它,我就會得到一個空白頁面。用戶必須手動刷新網頁才能看到網頁的內容。

回答

1

你必須在任何內容:

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) 
{ 
    // let the user access the main page 
    // i have made the main content with jquery mobile framework 
} 

所以..當你登錄,有什麼可看的。

它似乎沒有任何JavaScript元素的HTML元素來獲取和編輯句柄。除非你使用JavaScript來從頭開始創建元素。如果是這樣,你檢查過JavaScript是否已經加載?另外,請檢查控制檯中的錯誤。

+0

還有就是內容..我只是沒加吧..在..解釋內容時顯示我手動刷新頁面 – KMC

+0

只有HTML內容沒有加載.... JS加載....並沒有在控制檯上的錯誤.... – KMC

+0

那麼,是否有任何HTML內,如果聲明? –

0

試試這個代碼

<?php 
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) 
{ 
    echo "Welcome".$_SESSION['Username']; 
} 
elseif(!empty($_POST['username']) && !empty($_POST['password'])) 
{ 
    // let the user login 
     include "login.php"; 

} 
else 
{ 
    // display the login form 
<form method="post" action="index.php" name="loginform" id="loginform"> 
      <fieldset> 
       <label for="username">Username:</label><input type="text" name="username" id="username" /><br /> 
       <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> 
       <input type="submit" name="login" id="login" value="Login" /> 
      </fieldset> 
      </form> 
+0

當你使用'!empty($ _ SESSION ['LoggedIn'])''時,不需要'isset($ _ SESSION ['LoggedIn']]'' –

+0

有內容..我只是沒有添加它..在解釋..當我手動刷新頁面時顯示的內容 - – KMC

1

the link提到的頭(位置)必須被放置在頁面的頂部...所以我重新安排了,如果條件....

我在的index.php的頂部添加了此

if(!empty($_POST['username']) && !empty($_POST['password'])) 
{ 
    include "login.php"; 


} ?> 

然後在體內我加入其它if語句

if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) 
{ 
    // let the user access the main page 
    // i have made the main content with jquery mobile framework 
}else 
else 
{ 
    // display the login form 
<form method="post" action="index.php" name="loginform" id="loginform"> 
      <fieldset> 
       <label for="username">Username:</label><input type="text" name="username" id="username" /><br /> 
       <label for="password">Password:</label><input type="password" name="password" id="password" /><br /> 
       <input type="submit" name="login" id="login" value="Login" /> 
      </fieldset> 
      </form> 
} 

這已經解決了問題。

0

嘗試使用PHP_SELF對重定向在同一頁

header('Location:'.$_SERVER['PHP_SELF']); 

它可能會幫助

相關問題