简体   繁体   English

PHP中的会话(用户登录)

[英]Sessions in PHP (user login)

Im pretty new to PHP and am stuck on how to use sessions to check a username and password against a database. 我对PHP来说还很陌生,并停留在如何使用会话检查数据库的用户名和密码上。

I have a PHP file call createuser.php which allows me to add users to a database with a username and password from a html textbox. 我有一个PHP文件调用createuser.php,它允许我使用html文本框中的用户名和密码将用户添加到数据库中。 I have another file called viewuser.php which lets me type in a username and password and see if that user/password exists in the database. 我有另一个名为viewuser.php的文件,它允许我输入用户名和密码,并查看该用户/密码是否存在于数据库中。

How could I add sessions to checkuser which only lets the user use that file if the username and login is correct and is in the database. 我该如何将会话添加到checkuser,如果用户名和登录名正确且在数据库中,则该会话仅允许用户使用该文件。

Belor if my checkuser.php file. 相信我的checkuser.php文件。

<?php
 $username = $_POST['username']; 
 $password = $_POST['password']; 

 $db= new  PDO('sqlite:test2.db'); 
 $query = "SELECT * from login WHERE username = '$username' AND password = '$password' ";
 $results = $db->Query($query)->fetchAll(); 

 foreach ($results as $arow) {    
 print 'username: ' . $arow['username'] . '  password: ' . $arow['password'].'<br>';
 }

 $rowcount = count($results); 
 print "number of rows $rowcount <br>" ; 

 if ( $rowcount  > 0 ){
  print " details matched atleast once <br>";
 }else{
  print " details did not match <br>";    
 }
?>

 <form action='' method=post> 
    Username <input type='text' name='username' /> <br>
    Password <input type='text' name='password' /> <br>
    <input type='submit' value='Submit' /> 
 </form>

Here is how 这是怎么

....
$rowcount = mysql_num_rows($results); 
if ( $rowcount  > 0 )
{
 session_start();
 $_SESSION['user'] = $arow['username'];
 print " details matched atleast once <br>";
}

else
{
  print " details did not match <br>";
 }

 ?>
 ...

And in every other page 并在每隔一页

<?php
   session_start();
   if(!isset($_SESSION['user']))       
      //redirect to login
    else
     //logged in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM