我想实现的功能是,防止用户重复登录,太高深的方法我不懂,我只会用自己能理解的方法,高手见谅!
首先登录页
if(!$username){ $aa= '账号不存在'; }else if($password != $bb){ $aa= '密码错误'; }else if(!empty($_SESSION["username"])){ $aa= '该账号已登录'; }else{ session_start(); $_SESSION["md5"] = md5($time); $_SESSION["username"] = $account; DB::query("UPDATE ".DB::table('abc')." set a='$time' , b=' {$_SESSION["username"]}' , md5='{$_SESSION['md5']}' where name='$account'"); $aa= '成功登陆'; }
这样写有问题 似乎是没有session_start() 就使用$_SESSION["username"] 产生的问题?
如果删掉 }else if(!empty($_SESSION["username"])); 会好一些 但是任有其他问题
下面是登录跳转后的页面
session_start(); $md5 = $_SESSION["md5"]; $username = $_SESSION["username"]; $aa= DB::get_one("SELECT * FROM ".DB::table('abc')." WHERE b='$username' and md5='$md5'"); if(!$aa){ session_destroy(); echo "<script>alert('你掉线了');</script>"; echo "<script language='javascript' type='text/javascript'>" ; echo "window.location.href = 'index.php';" ; echo "</script>" ; exit () ; }}
现在的问题是 不管怎么登录 都会提示"掉线了",即时清空缓存也是一样!
如果删掉登录页的 }else if(!empty($_SESSION["username"])); 部分 会正常
但是,同浏览器多开的问题还是存在,不同的浏览器似乎有效!
本人自学小白,高手勿喷
求解!
回复讨论(解决方案)
最好贴上后报错的信息,然后如果判断$_SESSION时,如果是原生的话,是要先session_start(),当然判断的时候,最好是isset($_SESSION['xx'])是否存在的,你这问题很乱,查询也要先调试看看查询结果是什么,如果有错,那么看看查询语句最终是什么?,防止重复登录的话,用SESSION的话,只要判断SESSION是否存在,SESSION是否符合,就行了
你这个写得很乱啊
登录页 获取输入的用户名和密码,查询数据是否正确,正确就写入session,然后跳转到登录后的页面
登陆后的页面只要判断是否有该用户的session 数据就行了,若没有,就跳到登录页提示登录

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
