2015-07-13 177 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']); 

它可能会帮助

相关问题