search
HomeBackend DevelopmentPHP TutorialPHP设置cookie跳转页面后cookie值丢失解决办法

PHP设置cookie跳转页面后cookie值丢失

本帖最后由 opopen 于 2015-08-12 22:54:00 编辑 问题就是,我输入正确的用户名和密码,还是照样让我重新登陆
在index.php页面获取不到cookie里面的值
主页是判断是否设置用户名,没有设置就返回设置,大牛们帮忙看下什么情况!

我贴一下代码:
<br /><br /><?php<br />//Login.php<br />//清空cookies<br />function clearCookies(){<br />		setCookie('username','',time()-3600);  //删除cookies中标识符为username的变量的值<br />		setCookie('isLogin','',time()-3600);      //删除cookies中标识符为isLogin的变量的值 <br />		setCookie('userpass','',time()-3600);      //删除cookies中标识符为userpass的变量的值 <br />}<br />//判断用户是否登录<br />	//if($_GET['action']=='login')<br />//	{<br />		clearCookies();<br />		$inputname = $_GET['loginname'];<br />		$inputpass = $_GET['loginpass'];<br />		echo '获取到用户名后立即输出'.$inputname.'</br>';<br />		echo '获取到密码后立即输出'.$inputpass.'</br>';<br />		//插入数据库<br />		$host = "localhost";<br />		$user = "root";<br />		$password = "";<br />		$dbname = "test";<br />		$connection = mysql_connect($host,$user,$password);<br />		//echo $connection;<br />		if($connection)<br />		{<br />			//$password = md5($password);<br />			echo '此处是密码'.$inputpass.'</br>';<br />			echo '此处是用户名'.$inputname.'</br>';<br />			<br />			mysql_select_db($dbname,$connection);<br />			echo "数据库链接成功了".'</br>';<br />			$sql = "   SELECT * FROM  `user_info`  where `uname`='$inputname' and `upass` = '$inputpass' ";<br />			echo $sql;<br />			$result = mysql_query($sql);<br />				if($result)<br />				{<br />					$row = mysql_fetch_array($result);<br />					if($row)<br />					{<br />						var_dump($row);<br />						setcookie("username",$row['uname'],time()+3600*24,'/');<br />						setcookie("userpass",$row['upass'],time()+3600*24,'/');<br />						setCookie('isLogin','login',time()+3600*24,'/'); <br />						//echo $_COOKIE["isLogin"];<br />						//echo $checklogin;<br />						//echo $checkloginvalue;<br />						//$home_url = 'index.php';<br />						mysql_close($connection);<br />						//echo "<script language='javascript'> alert(\"欢迎您,${row}['uname']\");window.location='../index.html'</script>";<br />						header("location:index.php");<br />						<br />					}else<br />					{<br />						die("登录失败");<br />					}<br />				}<br />		}<br />		else{<br />				echo "数据库链接失败";<br />		}<br />		<br />	//}<br />	<br />?><br />

接下来是主页:index.php
<br /><?php<br /><br />	$username = $_COOKIE["username"]; <br />	var_dump($username);<br />	echo '此处输出Cookie里面的用户名'.$username;<br />	if($username=="" || $username==null)<br />	{<br />					echo"我进来了";<br />					//header("location:login.html");<br />					<br />					exit;<br />	}<br />?><br />

------解决思路----------------------
我试了下,可以登陆的,cookie也写入成功了,截图给你看
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version