Home Backend Development PHP Tutorial PHP设置cookie跳转页面后cookie值丢失解决办法

PHP设置cookie跳转页面后cookie值丢失解决办法

Jun 13, 2016 pm 12:25 PM
cookie echo nbsp setcookie

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

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

我贴一下代码:

1

<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 />

Copy after login

接下来是主页:index.php

1

<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 />

Copy after login

------解决思路----------------------
我试了下,可以登陆的,cookie也写入成功了,截图给你看
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

See all articles