2015-02-11 43 views
0

好日子給大家,我在這裏有一個小問題。 這裏是我的代碼登錄時總是重定向到Index.php

的index.php

<?php 
include('login.php'); // Includes Login Script 
?> 

<!DOCTYPE html> 
<html lang="en" class="bg-black"> 
<head> 
     <meta charset="UTF-8"> 
     <title>CIIS | Log in</title> 
     <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> 
     <!-- bootstrap 3.0.2 --> 
     <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"> 
     <!-- font Awesome --> 
     <link href="css/font-awesome.min.css" rel="stylesheet" type="text/css"> 
     <!-- Theme style --> 
     <link href="css/AdminLTE.css" rel="stylesheet" type="text/css"> 

     <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
     <!--[if lt IE 9]> 
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> 
     <![endif]--> 
    </head> 

<body class="bg-black"> 
</br></br></br> 
     <div class="form-box" id="login-box"> 
      <div class="header">Log In</div> 
      <form action="" method="post"> 
       <div class="body bg-gray"></br> 
        <div class="form-group"></br> 
         <input type="text" id="username" name="username" class="form-control" placeholder="Username"> 
        </div> 
        <div class="form-group"> 
         <input type="password" id="password" name="password" class="form-control" placeholder="Password"></br> 
        </div>   
        </br> 
        <input name="submit" value="Login" type="submit" class="btn bg-olive btn-block"> 
        </br> 
       </div> 



      </form> 

</body> 
</html> 

的login.php

<?php 
ini_set('display_errors', 1); 
error_reporting(~0); 
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 
$connection = mysql_connect("localhost", "root", ""); 
// 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 = mysql_select_db("ciis", $connection); 
// SQL query to fetch information of registerd users and finds user match. 
$query = mysql_query("select username,password from ciislogin where password='$password' AND username='$username'", $connection); 
$rows = mysql_num_rows($query); 
if ($rows == 1) { 
$_SESSION['login_user']=$username; // Initializing Session 

      switch ($username) 
      { 
      case "ciisdistrict5": 
      header("location: district5/Home.php"); 
      break; 

      case "ciisbagbag": 
      header("location:brgy_Bagbag/Home.php"); 
      break; 

      case "ciissanbartolome": 
      header("location:brgy_SanBartolome/Home.php");  
      break; 

      case "ciiscapri": 
      header("location:brgy_Capri/Home.php");  
      break; 

      case "ciisfairview": 
      header("location:brgy_Fairview/Home.php");  
      break; 

      case "ciisgreaterlagro": 
      header("location:brgy_GreaterLagro/Home.php");  
      break; 

      case "ciisgulod": 
      header("location:brgy_Gulod/Home.php");  
      break; 

      case "ciiskaligayahan": 
      header("location:brgy_Kaligayahan/Home.php");  
      break; 

      case "ciisnagkaisangnayon": 
      header("location:brgy_NagkaisangNayon/Home.php");  
      break; 

      case "ciisnorthfairview": 
      header("location:brgy_NorthFairview/Home.php");  
      break; 

      case "ciisnovaproper": 
      header("location:brgy_NovaProper/Home.php");  
      break; 

      case "ciispasongputik": 
      header("location:brgy_PasongPutik/Home.php");  
      break; 

      case "ciissanagustin": 
      header("location:brgy_SanAgustin/Home.php");  
      break; 

      case "ciisstalucia": 
      header("location:brgy_StaLucia/Home.php");  
      break; 

      case "ciisstamonica": 
      header("location:brgy_StaMonica/Home.php");  
      break; 
      } 

} else{ 
    $error = true; 
    echo ("Sorry Wrong Username or Password"); //if either field is empty 
    } 
mysql_close($connection); // Closing Connection 
} 
} 
?> 

session.php文件

<?php 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysql_select_db("ciis", $connection); 
session_start();// Starting Session 
// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from ciislogin where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['username']; 
if(!isset($login_session)){ 
mysql_close($connection); // Closing Connection 
header('Location: index.php'); // Redirecting To Home Page 
} 
?> 

說明 1.My的index.php是我登錄(輸入用戶名和密碼) 2.login.p hp和session.php是我的與數據庫通信的php命令。

我的問題是,當我嘗試運行這個,它工作完美,但即時通訊使用本地wammp,但是當我上傳這個在我的webhosting,並把正確的用戶名和密碼它總是重定向到index.php,而不是我想要的網頁。

有幫助嗎?

回答

0

你必須把你的的session_start()在第一行,,,爲總 檢查出來

的login.php:

<?php 
session_start(); // Starting Session 
ini_set('display_errors', 1); 
error_reporting(~0); 
$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 
$connection = mysql_connect("localhost", "root", ""); 
// 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 = mysql_select_db("ciis", $connection); 
// SQL query to fetch information of registerd users and finds user match. 
$query = mysql_query("select username,password from ciislogin where password='$password' AND username='$username'", $connection); 
$rows = mysql_num_rows($query); 
if ($rows == 1) { 
$_SESSION['login_user']=$username; // Initializing Session 

      switch ($username) 
      { 
      case "ciisdistrict5": 
      header("location: district5/Home.php"); 
      break; 

      case "ciisbagbag": 
      header("location:brgy_Bagbag/Home.php"); 
      break; 

      case "ciissanbartolome": 
      header("location:brgy_SanBartolome/Home.php");  
      break; 

      case "ciiscapri": 
      header("location:brgy_Capri/Home.php");  
      break; 

      case "ciisfairview": 
      header("location:brgy_Fairview/Home.php");  
      break; 

      case "ciisgreaterlagro": 
      header("location:brgy_GreaterLagro/Home.php");  
      break; 

      case "ciisgulod": 
      header("location:brgy_Gulod/Home.php");  
      break; 

      case "ciiskaligayahan": 
      header("location:brgy_Kaligayahan/Home.php");  
      break; 

      case "ciisnagkaisangnayon": 
      header("location:brgy_NagkaisangNayon/Home.php");  
      break; 

      case "ciisnorthfairview": 
      header("location:brgy_NorthFairview/Home.php");  
      break; 

      case "ciisnovaproper": 
      header("location:brgy_NovaProper/Home.php");  
      break; 

      case "ciispasongputik": 
      header("location:brgy_PasongPutik/Home.php");  
      break; 

      case "ciissanagustin": 
      header("location:brgy_SanAgustin/Home.php");  
      break; 

      case "ciisstalucia": 
      header("location:brgy_StaLucia/Home.php");  
      break; 

      case "ciisstamonica": 
      header("location:brgy_StaMonica/Home.php");  
      break; 
      } 

} else{ 
    $error = true; 
    echo ("Sorry Wrong Username or Password"); //if either field is empty 
    } 
mysql_close($connection); // Closing Connection 
} 
} 
?> 

session.php文件:

<?php 
session_start();// Starting Session 
// Establishing Connection with Server by passing server_name, user_id and password as a parameter 
$connection = mysql_connect("localhost", "root", ""); 
// Selecting Database 
$db = mysql_select_db("ciis", $connection); 

// Storing Session 
$user_check=$_SESSION['login_user']; 
// SQL Query To Fetch Complete Information Of User 
$ses_sql=mysql_query("select username from ciislogin where username='$user_check'", $connection); 
$row = mysql_fetch_assoc($ses_sql); 
$login_session =$row['username']; 
if(!isset($login_session)){ 
mysql_close($connection); // Closing Connection 
header('Location: index.php'); // Redirecting To Home Page 
} 
?> 
+1

我該如何解決這個問題先生?我會在哪裏放正確的代碼? – 2015-02-11 18:44:47

+0

@ PaulEdwardPagente99編輯 – 2015-02-11 18:48:58

+1

先生我嘗試你的代碼,但它不工作似乎當我的用戶名和密碼是正確的代碼去index.php – 2015-02-11 18:56:34