search

想在用户登入时用session保留他的用户名,一边在其他php文本里调用 ,但怎么写都不对,谁可以指导一下? 


session_start(); 
// 保存6小时 
$lifeTime = 6 * 3600; 

session_register ("nowuser") ; 
$username = htmlspecialchars($_POST['username']); 
$nowuser=$username; 
$password = $_POST['password']; 
$lastdate = date('Y-m-d'); 
include('conn.php'); 
mysql_query("set character set 'gbk'"); 
mysql_query("set names 'gbk'"); 

$sql = "select * from user where username='".$username."' and password='".$password."' "; 
$check_query=mysql_query($sql); 
$num=mysql_num_rows($check_query); 

if($num>0) 

echo "你好.$username.欢迎进入你的空间!"; 
echo "你最近一次的登入时间为.$lastdate"; 
echo"点击进入"; 

else { 
exit('登录失败!点击此处 返回 重试'); 


回复讨论(解决方案)

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

直接用$_SESSION吧。

而且你的session_register ("nowuser") ; 时,nowuser是空的。

我的php版本还是4.多的。。。。
还有nowuser不是接受$username的赋值了吗

从 php 4.1.10 开始,session 的操作就应该用 $_SESSION 数组
既然你已经有用 $_POST ,那么你的 php 版本就该在 4.1.10 以上了

你只给除了 session 变量的设置,并没有给出读取
session_start();
echo $_SESSION['nowuser'];

如果想直接 echo $nowuser;
则需要打开 register_globals 开关

我并没有要读取,我想在另一个php脚本里调用当前登入的登入者的用户名,这该怎么做。

你不读取如何知道用户名是否存在、是什么?

先用一个php接收提交的表单用户名和密码,如果用户名和密码都和数据库中的相同,那么我想在logi。php中通过session来记下这个 正在登入的用户的用户名,然后在其他的php文本里调用这个用户名,来实现把我下一步想增加的信息添加到已存在用户的数据库表下。这个思路用session来实现有问题吗?还是我误解session的用法了。如果误解了,你可以给我提供一个实现该功能的思路吗?

login.php
保存session值:
$_SESSION['uname'] = "admin";


其他页面调用
session_start();
echo $_SESSION['uname'];

我来补全一下:

login.php(保存session值)
session_start();
$_SESSION['uname'] = "admin";


其他页面调用(提取session值)
session_start();
echo $_SESSION['uname'];

你就把session_start理解成开启了浏览器范围的全局变量$_SESSION 就行了...

登入检查页面

<?php session_start(); $username = htmlspecialchars($_POST['username']); $password = $_POST['password']; $lastdate = date('Y-m-d'); include('conn.php'); mysql_query("set character set 'gbk'"); mysql_query("set names 'gbk'"); $sql = "select * from user where username='".$username."' and password='".$password."' "; $check_query=mysql_query($sql); $num=mysql_num_rows($check_query); if($num>0){ $_SESSION['nowuser'] = $username;echo "你好.$username.欢迎进入你的空间!"; echo "你最近一次的登入时间为.$lastdate"; echo"<a herf='indexin.html'>点击进入</a>"; } else { exit('登录失败!点击此处 <a href="javascript:history.back(-1);">返回</a> 重试'); } ?>


登入后的页面:
<?phpsession_start();if(isset($_SESSION['nowuser'])){    echo "你好".$_SESSION['nowuser']."欢迎进入你的空间!"; }else{    echo "未登入";}?>

谢谢楼上诸位,受用了。

我来补全一下:

login.php(保存session值)
session_start();
$_SESSION['uname'] = "admin";


其他页面调用(提取session值)
session_start();
echo $_SESSION['uname'];

你就把session_start理解成开启了浏览器范围的全局变量$_SESSION 就行了...



正解 ,结贴给分吧

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool