PHP登录脚本中的会话变量

时间:2017-02-18 13:16:33

标签: php database

我刚刚完成了一个完整的登录并在PHP中注册了systsem,但我的问题是我还没有使用任何会话。我是PHP的新手,我之前从未使用过会话。我想要做的是,当我点击登录按钮时,它的主页应该在该页面的正文中打开用户名...但由于该会话在header.inc.php中关联了代码我的主页无法进行load ..it显示当我点击登录按钮时找不到网页..可以任何人告诉我我的错误在哪里..这是我第一次在stackoverflow中提问。对于任何错误抱歉,并提前感谢。

在我的所有代码下面工作:  1. index.php主页

<code>
 <!doctype html>
<?php include( "./inc/header.inc.php"); ?>

<?php
$reg=@$_POST['reg'];
//declaring variables tp prevent error
 $fn="";// first name
 $ln="";//last name
 $un="";//username
 $em="";//email
 $em2="";//email2
 $pswd="";//password
 $pswd2="";//password2
 $d="";//sign up date
 $u_check="";//check if username exist


 //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 exist
$u_check = mysqli_query( $db_conx ,"SELECT username FROM user WHERE                                          `username = '$un'");`
        // count the amount of the row where username= $un
         $check = mysqli_num_rows($u_check);
             if($check == 0){
        //check all the field have been filled in

            //check password match
            if($pswd==$pswd2){
//Encrypt Password and Password2 using Md5 before sending to database     
                        $pwsd=md5($pswd);
                        $pwsd2=md5($pswd2);
                        $query=mysqli_query( $db_conx , "INSERT INTO user VALUES ('', '$un' , '$fn', '$ln' , '$em' , '$pswd' , '$d' , '0')");
                        die("<h2>Welcome To FriendsBook</h2> Login To your Account to Get Started...");
            }
                    else{
                   echo "Your Password Don'T Match!";
                     }  
                    }
               else{
                echo "UserName Already Taken..!";
                  } 

    }
    else{
   echo "Your Email Doesn't Match!..";
   }   

}

     // User Login code

   if(null!==(@$_POST["user_login"]) && null!==(@$_POST["password_login"])){

  $password_login_md5 = md5($password_login);
 $sql = mysqli_query($db_conx , "SELECT id FROM user WHERE username =     '$user_login' AND password = '$password_login_md5' LIMIT 1"); //query
 // check for existance
 $userCount = mysqli_num_rows($sql); //Count The Name Of and return
   if($userCount==1){
       while($row = mysqli_fetch_array($sql , MYSQLI_NUM)){
           $id = $row["id"];
       }
          session_start();
         $_SESSION["user_login"] = $user_login;
         header("location: home.php");
         exit();
       }else{
       echo 'That Information is Incorrect , Try Again !!';
       exit();
      }
   }



 ?>

      <div style="width:800px; margin:0px auto 0px auto;">
      <table>
      <tr>
        <td width="60%" valign="top">
              <h2>Already a Member? Sign in below! </h2>
              <form action="index.php" method="POST">
                 <input type="text" name="user_login" size="25" placeholder="UserName" /><br /><br />
                 <input type="text" name="password_login" size="25" placeholder="Password" /><br /><br />
                 <input type="submit" name="login" value="Login" >
              </form>





        </td>
        <td width="40%" valign="top">
            <h2>Sign Up Below</h2>
            <form action="#" method="POST">
                <input type="text" name="fname"  maxlength="25" placeholder="First Name"  title="Maximum Limit Of First Name is 25 Character" required /><br /><br />
                <input type="text" name="lname"  maxlength="25" placeholder="Last Name"  title="Maximum Limit Of Last Name is 25 Character"  required/><br /><br />
                <input type="text" name="username"  maxlength="25" placeholder="UserName"  title="Maximum Limit Of Username is 25 Character" required/><br /><br />

                <input type="email" name="email" maxlength="35" placeholder="Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"  required /><br /><br />


                <input type="email" name="email2" maxlength="35" placeholder="Re-Enter Email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required/><br /><br />

                <input type="password" name="password" min="8" maxlength="25" placeholder="Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" 
                title="Must contain at least one number and one uppercase and lowercase letter,
                and at least 8 or more characters" required /><br /><br />

                <input type="password" name="password2" min="8" maxlength="25" placeholder="Re-Enter Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" 
                title="Must contain at least one number and one uppercase and lowercase letter,
                and at least 8 or more characters" required/><br /><br />

                <input type="submit" name="reg" value="Sign Up!" >
             </form>
         </td>
        </tr>
      </table>

      <?php include( "./inc/footer.inc.php");?>`

2.header.inc.php代码

    <?php include( "./inc/connect.inc.php");
 session_start();
 // Store Session Data
  if(isset($_SESSION["user_login"])){
    header("location: home.php");
   }
  else
   }


 ?>



  <html>
  <head>
  <title>FriendsBook</title>
  <link rel="stylesheet" type="text/css" href="css/style.css"/>
  </head>
  <body>
      <div class="headerMenu">
         <div id="wrapper">
            <div class="logo">
               <img src="img/friendsbooklogo.PNG"/>
            </div>
            <div class="Search_box">
                 <form action="Search.php" method="GET" id="search">
                 <input type="text" name="q" size="60"    placeholder="Search..."/>
                 </form>
            </div>
         </div>
         <div id="menu">
            <a href="#">Home</a>
            <a href="#">About</a>
            <a href="#">Sign Up</a>
            <a href="#">Sign IN</a>

         </div>
      </div>
           </body>

3.connect.inc.php

  <?php
 $db_conx = mysqli_connect("localhost","root","" , "friendsbook" );
   ?>

4.logout.php

  <?php
  session_start();
 session_destroy();
 header("Location : index.php");
?>

5.home.php

 <?php

 include( "./inc/header.inc.php"); 
 echo $_SESSION["user_login"];

 ?>

0 个答案:

没有答案