How can I verify the cookie?

Source: Internet
Author: User
How can I verify the cookie? how can I verify the cookie?

Mysql_select_db ("dhbase", $ conn );
$ SQL = "select * from dh_admin where uname = '$ lname' and upass =' $ ckpass' and work = 1 ";
$ Result = mysql_query ($ SQL, $ conn );
If (mysql_num_rows ($ result) <= 0)
{
Echo "incorrect account information ";
Exit;
}
Else
{
$ Row = mysql_fetch_array ($ result );
$ Lid = $ row ["id"];
$ Ltime = date ('Y-m-d h: I: S', time ());
$ User_IP = @ ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];
$ Lsql = "insert into dh_alog (aid, logtime, logip) VALUES ('$ lid',' $ ltime', '$ user_IP ')";
If (! Mysql_query ($ lsql, $ conn ))
{
Die ('Error: '. mysql_error ());
}
Setcookie ("uname", $ lname, time () + 3600 );
// Echo $ _ COOKIE ["uname"];
Echo "script location. href = 'index _ home. php'; script";
Exit;
}
This step of login verification and writing cookie output cookie is also normal
Go to the index_home.php page, which calls the verification page admin_chk.php.
The code is as follows:
Session_start ();
If (isset ($ _ COOKIE ["uname"])
{
Echo $ _ COOKIE ["uname"];
}
Else
{
Echo "script alert ('Tips: Your permission has timed out. log on Again '); location. href = 'index. php' script";
Exit;
}
?>
Is this code wrong? As soon as you log on to debug, I am prompted: Your permission has timed out. please log on again.
Thank you for your advice.


Reply to discussion (solution)

The code is fine. how do you debug it. Test with multiple browsers.

The code is fine. how do you debug it. Test with multiple browsers.
Thank you. is PHP still related to browsers?

The code is fine. how do you debug it. Test with multiple browsers.
I have changed IE and Firefox.
I directly output $ _ COOKIE ["uname"] in admin_chk.php. the error message is "undefined.

What's going on? I just encountered such a strange thing when I was learning PHP.

I don't know how your admin_chk.ph code is written.

Is the cookie path correct?

Header ("Content-Type: text/html; charset = gb2312 ");
Session_start ();
If (! Isset ($ _ COOKIE ["uname"])
{
Echo "script alert ('Tips: Your permission has timed out. log on Again '); location. href = 'index. php' script";
Exit;
}
?>

This is the admin_chk.php file.

Index_home.php and admin_chk.php are not in the same directory.
Index_home.asp reference:
Include "inc/admin_chk.php"
?>
....


Have you gone to dinner ??

Index_home.php is the code. Is there any code above?

Document layout
Index_home.php
Inc/admin_chk.php

Inc/admin_chk.php contains setcookie ("uname", $ lname, time () + 3600 );

Is that true?

So
Setcookie ("uname", $ lname, time () + 3600 );
Ying writing
Setcookie ("uname", $ lname, time () + 3600 ,'/');


Index_home.php is the code. Is there any code above?
Some html code is missing.



Document layout
Index_home.php
Inc/admin_chk.php

Inc/admin_chk.php contains setcookie ("uname", $ lname, time () + 3600 );

Is that true?

So
Setcookie ("uname", $ lname, time () + 3600 );
Ying writing
Setcookie ("uname", $ lname, time () + 3600 ,'/');

Setcookie ("uname", $ lname, time () + 3600); this statement is written when login_ck.php is verified on login.

The logon interface is verified by ajax asynchronous login_ck.php:

Header ("Content-Type: text/html; charset = gb2312 ");
Session_start ();
$ Lname = $ _ POST ["Loname"];
If ($ lname = "")
{
Echo "enter the user name ";
Exit ();
}
$ Lpass = $ _ POST ["Lopass"];
If ($ lpass = ""){
Echo "enter the password ";
Exit ();
}
$ Lsx = $ _ POST ["Losx"];
If ($ lsx = "")
{
Echo "enter the password ";
Exit ();
}
If ($ lsx! = "Dhsystem ")
{
Echo "BCC error ";
Exit ();
}
$ Lcode = strtolower ($ _ POST ["Locode"]);
If ($ lcode = "")
{
Echo "Enter the verification code ";
Exit ();
}
If ($ lcode! = Strtolower ($ _ SESSION ["randcode"])
{
Echo "incorrect verification code input ";
Exit ();
}
$ Ckpass = substr (md5 ($ lpass), 9, 12 );
$ Conn = mysql_connect ("localhost", "root ","*******");
If (! $ Conn)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("dhbase", $ conn );
$ SQL = "select * from dh_admin where uname = '$ lname' and upass =' $ ckpass' and work = 1 ";
$ Result = mysql_query ($ SQL, $ conn );
If (mysql_num_rows ($ result) <= 0)
{
Echo "incorrect account information ";
Exit;
}
Else
{
$ Row = mysql_fetch_array ($ result );
$ Lid = $ row ["id"];
$ Ltime = date ('Y-m-d h: I: S', time ());
$ User_IP = @ ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];
$ Lsql = "insert into dh_alog (aid, logtime, logip) VALUES ('$ lid',' $ ltime', '$ user_IP ')";
If (! Mysql_query ($ lsql, $ conn ))
{
Die ('Error: '. mysql_error ());
}
Setcookie ("uname", $ lname, time () + 3600 );
// Echo $ _ COOKIE ["uname"];
Echo "script location. href = 'index _ home. php'; script";
Exit;
}

Mysql_close ($ conn );
?>

This is from login_ck.php.

Didn't your login request go to inc/admin_chk.php?
The cookie set in inc/admin_chk.php is valid only in the inc directory.

Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])

Parameter path
Indicates the valid path of the Cookie on the server.
If this parameter is set to '/', the cookie is valid throughout the domain. if it is set to '/foo /', the cookie is valid only in the/foo/directory under the domain and its subdirectories, for example,/foo/bar /. The default value is the current directory of the cookie.



I encountered a real path problem. I used asp PHP. This cookie still has a path problem.

Thank you.

Didn't your login request go to inc/admin_chk.php?
The cookie set in inc/admin_chk.php is valid only in the inc directory.

Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])

Parameter path
Indicates the valid path of the Cookie on the server.
If this parameter is set to '/', the cookie is valid throughout the domain. if it is set to '/foo /', the cookie is valid only in the/foo/directory under the domain and its subdirectories, for example,/foo/bar /. The default value is the current directory of the cookie.

Thank you.

I also learned

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.