2016-04-28 123 views
0

我想在登錄後重定向回index.php頁面,但我遇到了麻煩。登錄時重定向

這裏是我的login.php:

<?php 
session_start(); 
include_once 'dbconnect.php'; 

if(isset($_SESSION['user'])!=""){ 
    header("home.php"); 
} 
if(isset($_POST['btn-login'])){ 
    $email = mysql_real_escape_string($_POST['email']); 
    $upass = mysql_real_escape_string($_POST['pass']); 
    $res=mysql_query("SELECT * FROM users WHERE email='$email'"); 
    $row=mysql_fetch_array($res); 
    if($row['password']==md5($upass)) 
    { 
     $_SESSION['user'] = $row['user_id']; 
     header("home.php"); 
    }else{ 
    ?> 
    <script>alert('wrong details');</script> 
    <?php 
    } 

} 
?> 

這是我home.php:

<?php 
session_start(); 
include_once 'dbconnect.php'; 

if(!isset($_SESSION['user'])){ 
    header("Location: index.php"); 
} 
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']); 
$userRow=mysql_fetch_array($res); 
?> 

這是我在我的index.php導航欄:

<!-- Nav Bar --> 
<nav class="navbar navbar-default navbar-fixed-top navbar-left"> 
    <div class="container"> 
     <div class="navbar-header"> 
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
      </button> 
       <a class="navbar-brand brand" href="#" style="font-family: Warnes">Unleasht</a> 
     </div> 
    <div class="collapse navbar-collapse"> 
     <ul class="nav navbar-nav"> 
      <li><a href="#">HOME</a></li> 
      <li><a href="#about">ABOUT</a></li> 
      <li><a href="#music" data-toggle="collapse" data-target=".navbar-collapse.in">MUSIC</a></li> 
      <li><a href="#contact" data-toggle="collapse" data-target=".navbar-collapse.in">CONTACT</a></li> 
     </ul> 
     <ul class="nav navbar-nav navbar-right"> 

      <?php 
      function loggedIn(){ 
      if(isset($_SESSION['username'])!="") 
      { echo '<li class="dropdown"> 
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'. $_SESSION['username'] .'<span class="caret"></span></a> 
        <ul class="dropdown-menu"> 
         <li><a href="#">Action</a></li> 
         <li><a href="#">Another action</a></li> 
         <li><a href="#">Something else here</a></li> 
         <li role="separator" class="divider"></li> 
         <li><a href="#">Separated link</a></li> 
         <li role="separator" class="divider"></li> 
         <li><a href="#">One more separated link</a></li> 
        </ul> 
       </li>'; 
      }else{ 
       echo '<li><a href="#login" data-toggle="modal" data-target="#login" id="loginbtn"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>'; 
       } 
      } 
      loggedIn(); 
      ?> 

     </ul> 
    </div><!--/.nav-collapse --> 
    </div> 
</nav> 

但他們都不在同一個文件中。 login.php的位置在assets/php/login/login.php

我可以得到一些幫助嗎?

+1

** **危險:您正在使用[不合適的散列算法(http://php.net/manual/en/faq.passwords.php)和需要以更好地保護您的用戶密碼(https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet)。 – Quentin

+1

**警告**:您正在使用[**過時的**數據庫API](http://stackoverflow.com/q/12859942/19068),它已被**刪除**完全從最新版本的PHP 。你應該使用[現代替換](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 – Quentin

回答

0

您忘記了Location:設置標題以重定向。

你必須改變header("home.php");header("Location: home.php");

+0

它仍然無法正常工作。我試着把它改成header(「Location:../../../index.php」) –