2017-08-29 89 views
1

登录页面未proprly工作,如何设置会话

<?php 
 
session_Start(); 
 
if (isset($_POST['LOGIN'])) 
 
{ 
 
     $servername = "localhost"; 
 
     $username = "root"; 
 
     $password = ""; 
 
     $dbname = "mysite"; 
 
\t 
 
\t 
 
\t $conn = new mysqli($servername, $username, $password, $dbname); 
 
\t 
 
\t 
 
\t if ($conn->connect_error) 
 
\t 
 
\t 
 
    { 
 
     die("Connection failed: ".$conn->connect_error); 
 
    } 
 
\t else 
 
{  
 
\t $result1 = "SELECT userid, password FROM user WHERE username = '$username' And password = '$password'"; 
 
\t $queryResult = $conn->query($result1); 
 
\t } 
 
\t 
 
\t if ($queryResult > 0) 
 
\t { 
 
\t 
 
\t \t 
 
\t \t 
 
\t \t $_SESSION['username'] = $username; 
 
\t \t header("location: home.php"); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t echo 'The username or password are incorrect!'; 
 
\t } 
 
} 
 
?>
<style> 
 
.header{ 
 
width:100%; 
 
background-color:#00adff; 
 
float:left; 
 
color:white; 
 
    padding: 1%; 
 
} 
 
.rightChuck{ 
 
float:right; 
 
width:18%; 
 
} 
 
.menu{ 
 
float:left; 
 
padding:5%; 
 
} 
 
#title 
 
{ 
 
float:left; 
 
margin-left:10px; 
 
} 
 
body{ 
 
margin:0px !important; 
 
} 
 
h1{ 
 
margin: 0px !important; 
 
} 
 
#main 
 
{ 
 
text-align:left; 
 
} 
 
#content{ 
 
    
 
\t margin: auto; 
 
    position: absolute; 
 
    top: 100; 
 
    bottom: 100; 
 
    left: 100; 
 
    right: 100; 
 
    background: white; 
 
\t border: 5px solid green; 
 
    width: 400px; 
 
    height: 200px; 
 
\t 
 
} 
 
</style>
<html> 
 
<head> 
 
<title>Mysite.com</title> 
 
</head> 
 
<body> 
 

 
<div class="header"> 
 
<div id="title"> 
 
<h1>Mysite.com</h1> 
 
</div> 
 
<div class="rightChuck"> 
 
<div class="menu"> 
 
<a href="login.php">LOGIN </a> 
 
</div> 
 
<div class="menu"> 
 
<a href="register.php">REGISTER</a> 
 
</div> 
 
</div> 
 
</div> 
 

 

 
<div style="float:left;width:100%;padding:left:10px;"> 
 
<h2>LOGIN</h2> 
 
</div> 
 
<div id="content"> 
 
<center> 
 
<form name="form" method="post"> 
 
<table cellpadding="5" cellspacing="10"> 
 
<tr><th ><h3>USERNAME</h3></th><td><input type="text" placeholder="Username" name="username" size="20" id="textbox" required /></td></tr> 
 
<tr><th><h3>PASSWORD</h3></th><td><input type="password" placeholder="Password" id="password"name="password" size="20" id="textbox" required /></td></tr> 
 
</td> 
 
</table> 
 
<input type="submit" name="LOGIN" value="LOGIN" id="buttondesign" /> 
 
<br /> 
 
<br /> 
 

 
</form> 
 
</div> 
 

 

 
</body> 
 
</html>

这个登录页面不能正常工作时,正确的做法是,当我点击登录按钮,它的进入从DATABSE检查用户名和密码,如果用户名和密码是正确的,它的用户名和用户标识存储在会话中,它将转到主页,否则用户名和密码是错误的,它会出现错误消息。但是这个代码包含一些错误,比如我点击登录按钮它回到主页页面,而不是检查用户名和密码,那么会话不会被存储。在形式上

<form name="form" method="post"> 

+1

是在session_start();不是session_Start(); – Roy

回答

1
<html> 
    <head> 
    <title>Mysite.com</title> 
    </head> 
<body> 

<div class="header"> 
<div id="title"> 
<h1>Mysite.com</h1> 
</div> 
<div class="rightChuck"> 
<div class="menu"> 
<a href="login.php">LOGIN </a> 
</div> 
<div class="menu"> 
<a href="register.php">REGISTER</a> 
</div> 
</div> 
</div> 


<div style="float:left;width:100%;padding:left:10px;"> 
<h2>LOGIN</h2> 
</div> 
<div id="content"> 
<center> 
<form name="form" method="post" action=""> 
<table cellpadding="5" cellspacing="10"> 
<tr><th ><h3>USERNAME</h3></th><td><input type="text" placeholder="Username" name="username" size="20" id="textbox" required /></td></tr> 
<tr><th><h3>PASSWORD</h3></th><td><input type="password" placeholder="Password" id="password" name="password" size="20" id="textbox" required /></td></tr> 
</td> 
</table> 
<input type="submit" name="LOGIN" value="LOGIN" id="buttondesign" /> 
<br /> 
<br /> 

</form> 
</div> 


</body> 
</html> 
<?php 
session_Start(); 
if (isset($_POST['LOGIN'])) 
{ 
     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "mysite"; 
     $conn = mysqli_connect($servername, $username, $password, $dbname); 

if (mysqli_connect_errno())  
    { 

      die("Connection failed: ".$conn->connect_error); 
    } 
    else 
     { 

     $username=$_POST['username']; 
     $password=$_POST['password']; 


     $sql="SELECT `username`, `password` FROM `user` WHERE `username`= $username AND `password`=$password"; 

     if ($result=mysqli_query($conn,$sql)) 
      { 
      // Return the number of rows in result set 
      $rowcount=mysqli_num_rows($result); 

      if ($rowcount > 0) 
       { 

        $_SESSION['username'] = $username; 
        header("location: home.php"); 
       } 
       else 
       { 

        echo 'The username or password are incorrect!'; 
       } 
      } 

     } 
    //echo $rowcount; 


} 
?> 
1

放动作来

<form name="form" method="post" action="login.php"> 
0

你需要有主页上sessions_start太:

<?php session_start(); ?> 

尝试添加的用户名在SELECT查询。

$result1 = "SELECT userid, username, password FROM user WHERE username = '$username' And password = '$password'"; 

如果你想存储的用户ID也需要:

$_SESSION['userid'] = $userid; 
0

的 “S”,在 “开始” “在session_start()” 需要在较低案件。

session_Start(); // This would not initiate the session 

session_start(); // This would initiate the session 

由于这个会议是从未创建。

修复这应该解决这个问题

0
<?php 
session_Start(); 
if (isset($_POST['LOGIN'])) 
{ 
     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "mysite"; 
     $conn = new mysqli($servername, $username, $password, $dbname); 
     if ($conn->connect_error) 
     { 
     die("Connection failed: ".$conn->connect_error); 
     } 
    else 
    {  
    $result1 = "SELECT userid, password FROM user WHERE username = '$username' And password = '$password'"; 
     $queryResult= mysqli_query($conn, $sql); 
     $row = mysqli_fetch_array($queryResult); 
     $count = mysqli_num_rows($queryResult); // if uname/pass correct it returns must be 1 row 
    } 
     if ($count == 1 && $row['password'] == $password) { 
      $_SESSION['userid'] = $row['userid']; 
      header("Location: home.php"); 
     } 
     else { 
      $errMSG = "Incorrect Credentials, Try again..."; 
     } 
} 
?> 
相关问题