登录表单不会从 SQL 数据库获取信息并将其存储在会话变量中

login form won't get information from SQL database and store it in a session variable

我无法让我的登录脚本登录...我有一个 index.php 和一个注册表格和一个登录表格,注册表格工作得很好,但似乎没有登录表格当您输入 "login" 按钮时,数据库中的信息会在您登录时重定向到 "home.php",这将在会话的帮助下显示您的用户名。但我得到这个错误 "Notice: Undefined variable: username in home.php on line 12"... 我认为这是因为它没有登录并且会话得到一个未定义的变量。我就是找不到问题出在哪里

我有一个名为 "thesozializer" 的数据库 table 的 sql 是:

创建 TABLE 如果不存在 users ( id int(11) 不为空, username varchar(255) 不为空, first_name varchar(255) 不为空, last_name varchar(255) 不为空, email varchar(255) 不为空, password varchar(32) 不为空, sign_up_date 日期不为空, activated enum('0','1') 不为空 ) ENGINE=InnoDB AUTO_INCREMENT=14 默认字符集=latin1;

index.php 看起来像这样:

<?php 
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");

$reg = @$_POST['reg'];
//declaring variables to prevent errors
$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pswd = ""; //Password
$pswd2 = ""; // Password 2
$d = ""; // Sign up Date
$u_check = ""; // Check if username exists
//registration form
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d"); // Year - month - day

if ($reg) {
if ($em==$em2) {
// Check if user already exists
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
// Count the amount of rows where username = $un
$check = mysql_num_rows($u_check);
//Check whether Email already exists in the database
$e_check = mysql_query("SELECT email FROM users WHERE email='$em'");
//Count the number of rows returned
$email_check = mysql_num_rows($e_check);
if ($check == 0) {
  if ($email_check == 0) {
//check all of the fields have been filed in
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
// check that passwords match
if ($pswd==$pswd2) {
// check the maximum length of username/first name/last name does not exceed 25 characters
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "maximum length of username/first name/last name is 25 characters!";
}
else
{
// check the maximum length of password does not exceed 25 characters and is not less than 5 characters
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Password must be between 5 and 25 characters!";
}
else
{
//encrypt password and password 2 using md5 before sending to database
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
die("<h2>welcome to The Socializer!</h2>Login to get started");
}
}
}
else {
echo "your passwords is incorrect";
}
}
else
{
echo "fill in all fields";
}
}
else
{
 echo "email already in use";
}
}
else
{
echo "username already in use";
}
}
else {
echo "The emails is not alike!";
}
}

//User Login Code

if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
 $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]);
 $password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]);
 $password_login_md5 = md5($password_login);
$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$password_login_md5' LIMIT 1");
 //Check for their existance
 $userCount = mysql_num_rows($sql); //Count the number of rows returned
 if ($userCount == 1) {
  while($row = mysql_fetch_array($sql)){
   $id = $row["id"];
 }
  
  $_SESSION["user_login"] = $user_login;
  header("location: home.php");
  exit();
  } 
  else {
  echo 'username or password is incorrect';
  exit();
 }
}

session_start();
if (!isset($_SESSION["user_login"])) {

}
else
{
 $username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
 <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
   $(document).ready(function(){
    $("#registrer-deg").click(function(){
        $("#registrerdeg").show();
      });
      $("#registrer-deg").click(function(){
        $("#logginn").hide();
     });
    $("#logg-inn").click(function(){
        $("#logginn").show();
      });
      $("#logg-inn").click(function(){
        $("#registrerdeg").hide();
    }); 
   });
  </script>
  <link rel="stylesheet" type="text/css" href="main.css"/>
  <title>The Socializer</title>
 </head>
 <body>
  <div id="sidebarLeft">
   <div id="logo"></div>
   <ul>
    <li><a href="#" id="logg-inn">Login</a></li>
    <li><a href="#" id="registrer-deg">Register</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
   </ul>
  </div>
  <div id="timeline">
   <div id="registrering">
    <form id="registrerdeg" action="index.php" method="POST" style="display: none;">
     <input type="text" name="fname" size="10" placeholder="First name"><br/>
     <input type="text" name="lname" size="10" placeholder="Last name"><br/>
     <input type="text" name="username" size="10" placeholder="Username"><br/>
     <input type="text" name="email" size="10" placeholder="Email"><br/>
     <input type="text" name="email2" size="10" placeholder="Confirm email"><br/>
     <input type="text" name="password" size="10" placeholder="Password"><br/>
     <input type="text" name="password2" size="10" placeholder="Confirm Password"><br/>
     <input type="submit" name="reg" value="Registrer!">
    </form>
   </div>
   <div id="logg_inn">
    <form id="logginn" action="index.php" method="POST" style="display: none;">
     <input type="text" name="user_login" size="10" placeholder="Username"><br/>
     <input type="text" name="password_login" size="10" placeholder="Password"><br/>
     <input type="submit" name="login" value="Logg inn!">
    </form>
   </div>
  </div>
 </body>
</html>
* {
 background-color: #2C3E50;
 font-family: Arial, Helvetica, Sans-serif;
 font-size: 16px;
 color: #AFEEEE;
}

#sidebarLeft {
 width: 220px;
 height: 550px;
 top: 0;
 left: 0;
 margin-top: 50px;
 margin-left: 0px;
 margin-bottom: 50px;
 position: fixed;
}

#sidebarRight {
 width: 220px;
 height: 550px;
 right: 0;
 top: 0;
 margin-top: 50px;
 margin-right: 0px;
 margin-bottom: 50px;
 position: fixed;
}

ul {
 width: 220px;
    list-style-type: none;
    margin: 0px;
    padding: 0;
    margin-top: 30px;
}

li {
 height: 35px;
 width: 220px;
 list-style-type: none;
 margin: 5px;
}

#logo {
 width: 150px;
 height: 150px;
 background-image: url("../img/logo.png");
 -moz-border-radius: 75px;
 -webkit-border-radius: 750px;
 border-radius: 75px;
 margin-left: 35px;
 margin-top: 25px;
}

#sidebarLeft ul li a {
    display: block;
    width: 60px;
    width: 220px;
    height: 16px;
    text-align: center;
    margin-top: 9px;
    text-decoration: none;
    color: #AFEEEE;
}

#timeline {
 width: 780px;
 height: 550px;
 margin-top: 50px;
 margin-left: 240px;
 top: 0;
}

input[type="text"] {
 background-color: #FFFFFF;
 border: 1px solid #E2E2E2;
 color: #000000;
 font-size: 15px;
 font-weight: bold;
 padding: 5px;
 width: 200px;
 height: 12px;
 margin-bottom: 3px;
 margin-top: 3px;
 outline: none;
}

::-webkit-input-placeholder {
 font-weight: normal;
}

:-moz-input-placeholder {
 font-weight: normal;
}

::-moz-input-placeholder {
 font-weight: normal;
}

:-ms-input-placeholder {
 font-weight: normal;
}

input[type="submit"] {
   border-top: 1px solid #96d1f8;
   background: #61a6d4;
   background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
   background: -webkit-linear-gradient(top, #316c94, #61a6d4);
   background: -moz-linear-gradient(top, #316c94, #61a6d4);
   background: -ms-linear-gradient(top, #316c94, #61a6d4);
   background: -o-linear-gradient(top, #316c94, #61a6d4);
   padding: 5px 10px;
   -webkit-border-radius: 7px;
   -moz-border-radius: 7px;
   border-radius: 7px;
   -webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
   -moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
   box-shadow: rgba(0,0,0,1) 0 1px 0;
   text-shadow: rgba(0,0,0,.4) 0 1px 0;
   color: #ffffff;
   font-size: 12px;
   font-family: Helvetica, Arial, Sans-Serif;
   text-decoration: none;
   vertical-align: middle;
}

input[type="submit"]:hover {
   border-top-color: #49718c;
   background: #49718c;
   color: #ccc;
}

input[type="submit"]:active {
   border-top-color: #1b435e;
   background: #1b435e;
}

和 home.php 看起来像这样:

<?php 
mysql_connect("localhost","root","") or die("couldn't connect to database.");
mysql_select_db("thesocializer") or die("couldn't select database");

session_start();
if (!isset($_SESSION["user_login"])) {

}
else
{
 $username = $_SESSION["user_login"];
}
?>
<!DOCTYPE html>
<html>
 <head>
  <link rel="stylesheet" type="text/css" href="css/main.css"/>
  <title>The Socializer</title>
 </head>
 <body>
  <div id="sidebarLeft">
   <div id="logo">
   </div>
   <ul>
    <li><a href="#" id="logg-inn">Logg inn</a></li>
    <li><a href="#" id="registrer-deg">Registrer deg</a></li>
    <li><a href="#">Om</a></li>
    <li><a href="#">Kontakt</a></li>
   </ul>
  </div>
  <div id="timeline">
   <?php echo "Hello, ".$username; ?>
  </div>
 </body>
</html>
* {
 background-color: #2C3E50;
 font-family: Arial, Helvetica, Sans-serif;
 font-size: 16px;
 color: #AFEEEE;
}

#sidebarLeft {
 width: 220px;
 height: 550px;
 top: 0;
 left: 0;
 margin-top: 50px;
 margin-left: 0px;
 margin-bottom: 50px;
 position: fixed;
}

#sidebarRight {
 width: 220px;
 height: 550px;
 right: 0;
 top: 0;
 margin-top: 50px;
 margin-right: 0px;
 margin-bottom: 50px;
 position: fixed;
}

ul {
 width: 220px;
    list-style-type: none;
    margin: 0px;
    padding: 0;
    margin-top: 30px;
}

li {
 height: 35px;
 width: 220px;
 list-style-type: none;
 margin: 5px;
}

#logo {
 width: 150px;
 height: 150px;
 background-image: url("../img/logo.png");
 -moz-border-radius: 75px;
 -webkit-border-radius: 750px;
 border-radius: 75px;
 margin-left: 35px;
 margin-top: 25px;
}

#sidebarLeft ul li a {
    display: block;
    width: 60px;
    width: 220px;
    height: 16px;
    text-align: center;
    margin-top: 9px;
    text-decoration: none;
    color: #AFEEEE;
}

#timeline {
 width: 780px;
 height: 550px;
 margin-top: 50px;
 margin-left: 240px;
 top: 0;
}

input[type="text"] {
 background-color: #FFFFFF;
 border: 1px solid #E2E2E2;
 color: #000000;
 font-size: 15px;
 font-weight: bold;
 padding: 5px;
 width: 200px;
 height: 12px;
 margin-bottom: 3px;
 margin-top: 3px;
 outline: none;
}

::-webkit-input-placeholder {
 font-weight: normal;
}

:-moz-input-placeholder {
 font-weight: normal;
}

::-moz-input-placeholder {
 font-weight: normal;
}

:-ms-input-placeholder {
 font-weight: normal;
}

input[type="submit"] {
   border-top: 1px solid #96d1f8;
   background: #61a6d4;
   background: -webkit-gradient(linear, left top, left bottom, from(#316c94), to(#61a6d4));
   background: -webkit-linear-gradient(top, #316c94, #61a6d4);
   background: -moz-linear-gradient(top, #316c94, #61a6d4);
   background: -ms-linear-gradient(top, #316c94, #61a6d4);
   background: -o-linear-gradient(top, #316c94, #61a6d4);
   padding: 5px 10px;
   -webkit-border-radius: 7px;
   -moz-border-radius: 7px;
   border-radius: 7px;
   -webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
   -moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
   box-shadow: rgba(0,0,0,1) 0 1px 0;
   text-shadow: rgba(0,0,0,.4) 0 1px 0;
   color: #ffffff;
   font-size: 12px;
   font-family: Helvetica, Arial, Sans-Serif;
   text-decoration: none;
   vertical-align: middle;
}

input[type="submit"]:hover {
   border-top-color: #49718c;
   background: #49718c;
   color: #ccc;
}

input[type="submit"]:active {
   border-top-color: #1b435e;
   background: #1b435e;
}

你必须移动 session_start(); 在脚本开头的两页中。

index.php:

<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...

home.php:

<?php
session_start();
mysql_connect("localhost","root","") or die("couldn't connect to database.");
...