使用会话和PHP登录脚本中的会话变量 [英] Using sessions & session variables in a PHP Login Script

查看:66
本文介绍了使用会话和PHP登录脚本中的会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了在PHP中创建整个登录和注册systsem的操作,但是我的问题是我还没有使用任何会话.我是PHP的新手,以前从未使用过会话.我想做的是,在用户注册并填写登录表单后,他们仍将停留在同一页面上.因此,其中一部分将是会话登录的,另一部分将是其他的(用户尚未登录,请显示登录表单).谁能告诉我如何入门?

I have just finished creating an entire login and register systsem in PHP, but my problem is I haven't used any sessions yet. I'm kind of a newbie in PHP and I've never used sessions before. What I want to do is, after the user registers and fills out the login form, they will still stay on the same page. So, there will be one part of the which will be if the session is logged_in and the other part will be else (the user is not logged in so display the login form). Can anyone tell me how to get started?

推荐答案

希望这会有所帮助:)

在会话开始之前,您需要在页面顶部或在调用会话代码之前说出这一点

begins the session, you need to say this at the top of a page or before you call session code

 session_start(); 

在会话中输入用户ID来跟踪谁登录

put a user id in the session to track who is logged in

 $_SESSION['user'] = $user_id;

检查是否有人登录

 if (isset($_SESSION['user'])) {
   // logged in
 } else {
   // not logged in
 }

找到已登录的用户ID

Find the logged in user ID

$_SESSION['user']

所以在您的页面上

 <?php
 session_start();


 if (isset($_SESSION['user'])) {
 ?>
   logged in HTML and code here
 <?php

 } else {
   ?>
   Not logged in HTML and code here
   <?php
 }

这篇关于使用会话和PHP登录脚本中的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆