我怎样才能这样做才能注册或登录我的代码?

时间:2015-12-15 00:16:11

标签: php mysql login registration

我应该使用基本的php,html和mysql来完成学校作业。

我在线阅读了一个教程,并更改了代码的各个方面,但我无法“注册”或“登录”,因此我可以查看其他页面。

这是我的索引页。

 <?php 
 session_start();
 include_once '../db_connect.php';

 //starts the session
 if(isset($_SESSION['login'])!="") {
header("Location: home.php");
 }

 if(isset($_POST['btn-login'])) {
     $email = mysqli_real_escape_string($_POST['email']);
     $upass = mysqli_real_escape_string($_POST['upass']);
     $res = mysqli_query("SELECT * FROM users WHERE email='$email'");
     $row = mysqli_fetch_array($res);

     if($row['password']==md5($upass)) {
         $_SESSION['user'] = $row['user_id'];
         header("Location: home.php");
     } else {

         echo "<p>You entered in the wrong information. Please re-enter.</p>\n";
     }
 }

 ?>

 <!doctype html>
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <title>Login</title>
 </head>

 <body>
 <main>
 <div id="login-form">
 <form method="post">
 <table align="center" width="30%" border="0">
     <tr>
         <td><input type="text" name="email" placeholder="Your Email" required /></td>
     </tr>
     <tr>
         <td><input type="password" name="upass" placeholder="Your Password" required /></td>
     </tr>
     <tr>
         <td><button type="submit" name="btn-login">Sign In</button></td>
     </tr>
     <tr>
         <td><a href="register.php">Sign Up Here</a></td>
     </tr>
 </table>
 </form>
 </div>
 </main>
 </body>
 </html>

这是我的注册页面:

 <?php

     session_start();
     if(isset($_SESSION['login'])!="") {
         header("Location: home.php");
     }

     include_once '../db_connect.php';

     if(isset($_POST['btn-signup'])) {
         $name = mysqli_real_escape_string($_POST['name']);
         $uname = mysqli_real_escape_string($_POST['uname']);
         $email = mysqli_real_escape_string($_POST['email']);
         $upass = mysqli_real_escape_string($_POST['upass']);

         if(mysqli_query("INSERT INTO users(name, uname, email, upass) VALUES('$name', $uname', '$email', '$upass')")) {

             echo "<p>Thank you $uname for registering with us!\n</p>";


         } else {
             echo "<p>Could not open a connection to the mySQL server due to error: <strong>" . mysqli_connect_error($db_connect)."</strong></p>";

             echo "<p>Sorry Guest, but you were unable to register. Please try again later.\n</p>";


         } //end mysql_query if statement
     } //end btn-signup if statement


 ?>
 <!doctype html>
 <html xmlns="http://www.3.org/1999/xhtml">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <title>Login and Registration System</title>
 </head>

 <body>

 <main>
 <div id="login-form">
 <form method="post">
 <table align="center" width="30%" border="0">
     <tr>
         <td><input type="text" name="name" placeholder="First Name" required /></td>
     </tr>
     <tr>
         <td><input type="text" name="uname" placeholder="User Name" required /></td>
     </tr>
     <tr>
         <td><input type="email" name="email" placeholder="Your Email" required /></td>
     </tr>
     <tr>    
         <td><input type="password" name="upass" placeholder="Your Password" required /></td>
     </tr>
     <tr>
         <td><button type="submit" name="btn-signup">Sign Me Up</button></td>
     </tr>
     <tr>
         <td><a href="index.php">Sign In Here</a></td>
     </tr>
 </table>
 </form>
 </div>
 </main>
 </body>
 </html>

这是主页:

 <?php
 session_start();
 include_once '../db_connect.php';

 //starts the session
 if(!isset($_SESSION['login'])) {
      header("Location: index.php");
 }




 $res=mysqli_query("SELECT * FROM users WHERE user_id=".$_SESSION['login']);
 $userRow=mysqli_fetch_array($res);


 ?>

 <!doctype html>
 <html>
 <head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <title>WELCOME! <?php echo $userROW['email']; ?></title>
</head>

 <body>
     <div id="header">
         <div id="left">
             <label>cleartuts</label>
         </div>
         <div id="right">
             <div id="content">
                 hi' <?php echo $userRow['uname']; ?>&nbsp; <a href="logout.php?logout">Sign Out</a>
              </div>
         </div>
     </div>        
 </body>
 </html>

退出页面:

 <?php 
 session_start();

 if(!isset($_SESSION['login'])) {
     header("Location: index.php");
  } else if (isset($_SESSION['user'])!="") {
     header("Location: home.php");
  }

 if(isset($_GET['logout'])) {
     session_destroy();
     unset($_SESSION['login']);
     header("Location: index.php");
 }

 ?>

最后是db_connect脚本:

 <?php 

 //defining constants

 //NOTe: once you upload your site to the Internet, you must create a proper user (never use root)
 //with a secure password, what follows is only for your test site:

  DEFINE ('DB_USER', 'root'); //username to access the mySQL server
  DEFINE ('DB_PASSWORD', 'root'); //password for that username
  DEFINE ('DB_HOST', 'localhost'); //in most situations your host will be 'localhost'
  DEFINE ('DB_NAME', 'login'); //this the the name of the database you want ot connect to

   //connecting to the mySQL server
  $db_connect = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

  if (!empty($db_connect)) {//see if we connected successfully
      //if we opened a connection to the mySQL server,
      //check to see if we can access the database

     if(!mysqli_select_db ($db_connect, DB_NAME)) {

         //if we can NOT access the database give out an error

         //debugging error, comment out when site goes live
         echo "<p>Error Number: <strong>". mysqli_errno($db_connect)."</strong>. Could not select the database: <strong>". DB_NAME."</strong>, due to error: <strong>". mysqli_error($db_connect)."</strong></p>\n";

         //user friendly error message with no technical details
         echo "<p>The site is currently experiencing technical difficulties. We apologize for any inconvenience. Please try again later.</p>\n";

         //if you have a footer include for your site, uncomment this
         //include_once ('includes/footer.html');

         exit(); //exits the script

     } // end if !mysqli_select_db (DB_NAME)

  } else { //could not connect to the mySQL server

     //debugging error, comment out when site goes live
     echo "<p>Could not open a connection to the mySQL server due to error: <strong>" . mysqli_connect_error($db_connect)."</strong></p>";

     //user friendly error message with no technical details
     echo "<p>The site is currently experiencing technical difficulties. We apologize for any inconvenience. Please try afain later.</p>\n";

     //if you have a footer include for your site, uncomment this
     //include_once ('includes/footer.html');

     exit(); //exit the script

  } //and else statement

  //if all goes well then the page that called this connection script will 
  //continue to run, if not, one of the exit statements above stopped it.


  ?>

This is my mySQL code

我不确定为什么不让我登录...帮忙?

1 个答案:

答案 0 :(得分:0)

当用户尝试登录时,您在$_SESSION['user']页面上设置了index.php,但在每个其他页面上,您都会检查是否设置了$_SESSION['login']

我只建议使用一个$_SESSION值并将其用于每个页面。