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

查看:39
本文介绍了使用会话和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?

推荐答案

希望这会有所帮助 :)

Hope this helps :)

开始会话,你需要在页面顶部或调用会话代码之前说这个

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

$_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天全站免登陆