2015-09-02 41 views
0

我創建登錄系統在php.when我填寫用戶名和密碼,會話啓動但頁面不重定向到profile.php我想登錄後,登錄頁面是刷新本身。但是當我手動刷新login.php然後重定向到profile.ph。 我登錄的代碼是:成功登錄頁面不重定向到新代碼在php代碼

if ($result->num_rows != 1) { 
    //echo "Invalid credentials...!"; 
    $message = "wrong credentials"; 
    echo "<script type='text/javascript'>alert('$message');</script>"; 
} else { 
    // Authenticated, set session variables 
    $user = $result->fetch_array(); 
    $_SESSION['user_id'] = $user['id']; 
    $_SESSION['username'] = $user['username']; 


    $message = "You have successfully logged in.."; 
    echo "<script type='text/javascript'>alert('$message');</script>"; 

    redirect_to("profile.php?id={$_SESSION['user_id']}"); 

} 
+0

你需要顯示你的redirect_to函數的代碼。 –

+0

你需要使用頭函數重定向的更多信息有看這裏http://php.net/manual/en/function.header。php –

+0

函數redirect_to($ url){ \t \t header(「Location:{$ url}」); \t} –

回答

0

使用

redirect("profile.php?id={$_SESSION['user_id']}"); 

http_redirect("profile.php", array("id" => "$_SESSION['user_id']"), true, HTTP_REDIRECT_PERM); 

header("profile.php?id={$_SESSION['user_id']}"); 

一些有用的鏈接

  1. header
  2. http_redirect
  3. How to make a redirect in PHP?
+0

它看起來像OP的函數'redirect_to()'使用'header(「Location:...」);'根據OP的評論 - > http:// stackoverflow.com/questions/32344612/after-successful-login-page-is-not-redirecting-to-new-page-in-php-code#comment52563313_32344612 – Sean

+0

感謝您的建議,但沒有人選擇與me.login頁面一起使用重定向後不刷新。 –

+0

檢查是否有'$ message',然後重定向頁面 –

-2

,請複製粘貼完整的代碼,否則嘗試用下面的行替換

redirect_to("profile.php?id={$_SESSION['user_id']}"); 

..

header("location:profile.php?id=".$_SESSION['user_id']); 

不要忘記在你的php代碼的頂部添加以下行。

ob_start(); 
+0

<?php ob_start();如果(isset($ _ POST ['submit'])){ \t $ email = $ _POST ['email']; \t $ password = $ _POST ['password']; \t \t $ sql =「選擇*從用戶的電子郵件LIKE'{$ email}'和密碼LIKE'{$ password}'LIMIT 1」; \t $ result = $ mysqli-> query($ sql); \t \t 如果($ result-> NUM_ROWS = 1!){ \t \t回聲 「

錯誤:無效的用戶名/密碼組合

」; \t}否則{ \t \t //身份驗證,設定會話變量 \t \t $用戶= $ result-> fetch_array(); \t \t $ _SESSION ['user_id'] = $ user ['id']; \t \t $ _SESSION ['username'] = $ user ['username']; \t header(「location:profile.php?id =」。$ _ SESSION ['user_id']); } } ?> –

+0

如何在任何條件爲真後刷新頁面? –

+0

你想刷新頁面還是重定向頁面? – phpnerd

-1
<?php 
ob_start(); 
if (isset ($_POST ['submit'])) { 

    $email = $_POST ['email']; 
    $password = $_POST ['password']; 
    $sql = "SELECT * from users WHERE email='$email' AND password='$password' limit 1"; 
    $result = mysql_query ($sql); 
    $num = mysql_num_rows ($result); 
    if ($num > 0) { 
     while ($rows = mysql_fetch_array ($result)) { 

      $username = $rows ['username']; 
      $uid = $rows ['user_id']; 
     } 
     session_start(); 
     $_SESSION ['user_id'] = $user ['id']; 
     $_SESSION ['username'] = $user ['username']; 
     header ("location:profile.php?id=" . $_SESSION ['user_id']); 
    } else { 
     echo "<p><b>Error:</b> Invalid username/password combination</p>"; 
    } 
} 
?> 

,如果你正在處理你的登錄頁面這個PHP代碼本身,那麼請充分代碼的HTML。因爲自動刷新可能是html代碼本身的問題。

0

PHP標題重定向不要輸出後發送。而你在這裏

$message = "You have successfully logged in.."; 
echo "<script type='text/javascript'>alert('$message');</script>"; 

發送它根據您的意見,您的功能確實使用了頭重定向。

function redirect_to ($url) { header("Location: {$url}"); } 

由於上面解釋的原因,這將不起作用。

使用JavaScript重定向那裏。

echo "<script>location.href='profile.php?id={$_SESSION['user_id']}';</script>"; 
0

正如您對問題的評論所述,標題重定向必須是瀏覽器的第一件事。所以你需要刪除echo語句。

if ($result->num_rows != 1) { 
    //echo "Invalid credentials...!"; 
    $message = "wrong credentials"; 
    echo "<script type='text/javascript'>alert('$message');</script>"; 
} else { 
    // Authenticated, set session variables 
    $user = $result->fetch_array(); 
    $_SESSION['user_id'] = $user['id']; 
    $_SESSION['username'] = $user['username']; 

    redirect_to("profile.php?id={$_SESSION['user_id']}"); 
} 
+0

hello every body echo「」; 適合我。謝謝..... –