登录时始终重定向到Index.php

时间:2015-02-11 18:24:39

标签: 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
}
?>

解释 我的index.php是我登录的地方(输入用户名和密码) 2.login.php和session.php是我与数据库通信的php命令。

我的问题是,当我尝试运行它时,它工作得很完美但我使用本地wammp但是当我在我的网站主机上传这个并输入正确的用户名和密码时,它总是将我重定向到index.php而不是我想要的网页

任何帮助?

1 个答案:

答案 0 :(得分: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
}
?>
相关问题