简体   繁体   English

如何检查用户是否在php中登录。 初学者

[英]how to check if a user is logged on in php. beginner

using mysql as database. 使用mysql作为数据库。 I got this code from the previous answers to the same question: 我从先前对同一问题的答案中获得了这段代码:

   session_start()):

   if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
   echo "Welcome to the member's area, " . $_SESSION['username'] . "!";
  } else {
     echo "Please log in first to see this page.";
     }

Could you please explain what is: $_SESSION['loggedin'] . 您能否解释一下是什么:$ _SESSION ['loggedin']。 Where could I define it? 我在哪里可以定义它? the loggedin, please help 登录后,请帮助

http://www.php.net/manual/book.session.php http://www.php.net/manual/book.session.php

I hope it will help you ;) 希望对您有帮助;)

$_SESSION is a super-global array (available anywhere) that store all sessions variables. $ _SESSION是一个超全局数组(可在任何地方使用),用于存储所有会话变量。

session_start(); // begins session

$_SESSION['user_id'] = 99;

So, the loggedin variable is set to true when a user logged in, and then it is stored in the session. 因此,当用户登录时,loginin变量将设置为true,然后将其存储在会话中。 Sessions are basically information that are saved on the server. 会话基本上是保存在服务器上的信息。

$_SESSION is simply a persistent container where you can store anything and retrieve it in other requests during the same session. $_SESSION只是一个持久性容器,您可以在其中存储任何内容并在同一会话中的其他请求中检索它。 As such, you would have to set $_SESSION['loggedin'] and $_SESSION['username'] at the point where the user has successfully logged in. 因此,您必须在用户成功登录时设置$_SESSION['loggedin']$_SESSION['username']

After login: 登录后:

$_SESSION['loggedin'] = true;

That's it. 而已。

You use sessions to store userdata to pass it between all pages that get loaded. 您可以使用会话存储用户数据,以在加载的所有页面之间传递用户数据。 You can define it as said by others by using the $_SESSION['sessionname'] var. 您可以使用$_SESSION['sessionname'] var来定义它,正如其他人所说的那样。

I will post a simple script below how to let people login on the website since you wanted to know how to use it: 由于您想知道如何使用它,我将在下面如何让人们登录网站的下方发布一个简单的脚本:

session_start(); #session start alwas needs to come first

//Lets make sure scriptkiddies stay out
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

//Read the user from the database with there credentials
$query = mysql_query("select id from user where username = $username and password = $password");

//Lets check if there is any match
if(mysql_num_rows($query) > 0)
{
    //if there is a match lets make the sessions to let the user login
    $_SESSION['loggedin'] = true;
    $_SESSION['username'] = $username;
}

This is a simple script how to use a Session for a login system. 这是一个简单的脚本,说明如何将会话用于登录系统。 There are many other ways you can use sessions 您可以使用多种其他方式使用会话

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

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