2016-01-15 135 views
-1

我嘗試在登錄表單中單擊提交按鈕後使用header("location: profile.php")重定向頁面,但該頁面不會重定向。用戶名和密碼只是在某處提交。重定向標題不會重定向到目標頁面

這是我的主網頁:

<?php 
include ($_SERVER["DOCUMENT_ROOT"]."/example/login.php"); 
if(isset($_SESSION['login_user'])){ 
header("location: profile.php"); 
} 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>welcome to noteshare</title> 
<meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="css/bootstrap.min.css"> 
    <link rel="stylesheet" href="css/bootstrap.css"> 
    <link rel="stylesheet" href="css/bootstrap-theme.css"> 
    <link rel="stylesheet" href="css/bootstrap-theme.min.css"> 
    <link rel="stylesheet" href="css/jquery_popup.css" /> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="js/bootstrap.js"></script> 
    <script src="js/npm.js"></script> 
    <script src="js/jquery_popup.js"></script> 
    <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> 
    <script type="text/javascript" src="js/jquery.leanModal.min.js"></script> 
    <style> 
    /* Remove the navbar's default margin-bottom and rounded borders */ 
    .navbar { 
     margin-bottom: 0; 
     border-radius: 0; 
    } 

    /* Set height of the grid so .sidenav can be 100% (adjust as needed) */ 
    .row.content {height: 450px} 

    /* Set gray background color and 100% height */ 
    .sidenav { 
     padding-top: 20px; 
     background-color: #f1f1f1; 
     height: 100%; 
    } 

    /* Set black background color, white text and some padding */ 
    footer { 
     background-color: #555; 
     color: white; 
     padding: 15px; 
    } 

    /* On small screens, set height to 'auto' for sidenav and grid */ 
    @media screen and (max-width: 767px) { 
     .sidenav { 
     height: auto; 
     padding: 15px; 
     } 
     .row.content {height:auto;} 
    }</style> 
</head> 

<!-- Body Starts Here --> 
<body id="body"> 
<nav class="navbar navbar-inverse"> 
    <div class="container-fluid"> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle" data-toggle="collapse"  data-target="#myNavbar"> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span>       
     </button> 
     <a class="navbar-brand" href="#">Logo</a> 
    </div> 
    <div class="collapse navbar-collapse" id="myNavbar"> 
     <ul class="nav navbar-nav"> 
     <li class="active"><a href="#">Home</a></li> 
     <li><a href="#">About</a></li> 
     <li><a href="#">Projects</a></li> 
     <li><a href="#">Contact</a></li> 
     </ul> 
     <ul class="nav navbar-nav navbar-right"> 
     <li><a href="#" id="popup" onclick="div_show()"><span class="glyphicon glyphicon-log-in"></span>Login</a></li> 
     </ul> 
    </div> 
    </div> 
</nav> 

<div class="container-fluid text-center">  
    <div class="row content"> 
    <div class="col-sm-2 sidenav"> 
     <p><a href="#">Link</a></p> 
     <p><a href="#">Link</a></p> 
     <p><a href="#">Link</a></p> 
    </div> 
    <div class="col-sm-8 text-left"> 
     <h1>noteshare</h1> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididuntut labore et dolore magna aliqua. Ut enim ad minim veniam, 
     quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui 
     officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> 
     <hr> 
     <img src="images/girl_studying.jpg"> 
    </div> 
    <div class="col-sm-2 sidenav"> 
     <div class="well"> 
     <p>ADS</p> 
     </div> 
     <div class="well"> 
     <p>ADS</p> 
     </div> 
    </div> 
    </div> 
</div> 

<footer class="container-fluid text-center"> 
    <p>Footer Text</p> 
</footer> 
<div id="abc"> 
<!-- Popup Div Starts Here --> 
<div id="popuplogin"> 
<!-- Contact Us Form --> 
<form action="" id="form" method="post" name="form"> 
<img id="close" src="images/cross.png" onclick ="div_hide()"> 
<h2>login form</h2> 
<hr id="line"> 
<input id="username" name="username" placeholder="username" type="text" required> 
<input id="password" name="password" placeholder="password" type="password" required> 
<input name="submit" type="submit" value=" Login "> 
<span><?php echo $error; ?></span> 
<p> <a href="register.php">click here to register first</a></p> 
</form> 
</div> 
<!-- Popup Div Ends Here --> 
</div> 
<!-- Display Popup Button --> 
</body> 
<!-- Body Ends Here --> 
</html> 

這裏是包含header()函數調用的login.php頁面:

<?php 
    session_start(); // Starting Session 
    $error=""; // Variable To Store Error Message 
    if (isset($_POST['submit'])) { 
    if (empty($_POST['username']) || empty($_POST['password'])) { 
    $error = "Username or Password is invalid"; 
    } 
    else 
    { 
    // Define $username and $password 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter 
    $server="localhost"; 
    $username="root"; 
    $password=""; 
    $database="noteshare"; 
    $conn = new mysqli($server, $username, $password, $database); 
    // To protect MySQL injection for Security purpose 
    $username = stripslashes($username); 
    $password = stripslashes($password); 
    //$username = mysql_real_escape_string($username); 
    //$password = mysql_real_escape_string($password); 
    // Selecting Database 
    //$db = mysqli_select_db("noteshare", $conn); 
    // SQL query to fetch information of registerd users and finds user match. 
    $qry="select * from signin where password='$password' AND username='$username'"; 
    $query = mysqli_query($conn,$qry); 
    $rows = mysqli_fetch_row($query); 
    if ($rows == 1) { 
    $_SESSION['login_user']=$username; // Initializing Session 
    header("Location:http://localhost.com/profile.php", true, 301); exit;// Redirecting To Other Page 
    } else { 
    $error = "Username or Password is invalid"; 
    } 
    mysqli_close($conn); // Closing Connection 
    } 
    } 
    ?> 
+0

我認爲條件if($ row == 1)計算結果爲false。嘗試if($ row) –

回答

0

在MySQL的連接是好的,那就試試這個,

header('Location:filename'); 
 
Eg: header('Location:test1.php');

0

函數mysqli_fetch_row返回一個數組或NULL。因此,

if ($rows == 1) 

永遠不會是真的,你不會到達帶有header()的重定向。

您可以使用即if (is_array($rows))if (isset($rows))

您可以隨時插入的var_dump($行)進行調試,看變量$行的樣子。

請注意,HTTP代碼301是永久重定向。它應該是302或者乾脆可以省略header()函數的第二個和第三個參數,這將是一個302

0

試試這個

header("Location:profile.php"); die(); 

或與JS

echo '<script>window.location.href = "profile.php";</script>' ; 
重定向
0

您正在使用絕對URL。在本地服務器上工作時不使用TLD .com。

變化:

header("Location:http://localhost.com/profile.php"); 

要:

header("Location:http://localhost/profile.php"); 

它應該工作!