带会话的Php登录系统

时间:2015-11-18 13:08:20

标签: php forms session login

我正在使用会话制作登录表单但是。我有一个问题。

问题是。当我按下提交按钮时,我发送到" login.php"而不是" office.php"。还有"错误"变量是空的。所以我不知道问题是什么。

的index.php

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

if(isset($_SESSION['login_user'])){
header("location: office.php");
}
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Login Form in PHP with Session</title>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div id="main">
      <h1>PHP Login Session Example</h1>
      <div id="login">
        <h2>Login Form</h2>
        <form method="post" action="login.php">
          <input type="hidden" name="submitted" id="submitted" value="1">
          <div class="form-group">
            <input type="text" class="form-control" name="username" id="username" placeholder="Username" required>
          </div>
          <div class="form-group">
            <input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
          </div>
          <div class="form-group">
            <input type="submit" value="Submit" class="btn btn-default">
          </div>
        </form>
        <span><?php echo $error; ?></span>
      </div>
    </div>
  </body>
</html>

的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
    $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("businesslife", $connection);
    // SQL query to fetch information of registerd users and finds user match.
    $query = mysql_query("select * from users where Use_Pass='$password' AND Use_Name='$username'");
    $rows = mysql_num_rows($query);
    if ($rows == 1) {
      $_SESSION['login_user']=$username; // Initializing Session
      header("location: office.php"); // Redirecting To Other Page
    } 
    else {
      $error = "Username or Password is invalid";
    }
    mysql_close($connection); // Closing Connection
  }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <span><?php echo $error; ?></span>
    </body>
</html>

office.php

<?php
  include('session.php');
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Your Home Page</title>
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <div id="profile">
      <b id="welcome">Welcome : <i><?php echo $login_session; ?></i></b>
      <b id="logout"><a href="logout.php">Log Out</a></b>
    </div>
  </body>
</html>

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("businesslife", $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 login 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
  }
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>

    </body>
</html>

我真的很感谢你的帮助

0 个答案:

没有答案