Introduction to cookies and sessions in PHP

Source: Internet
Author: User
Tags exit header http authentication php script sessions setcookie valid in domain

There are three ways to use PHP for user authentication: HTTP authentication, session usage, and use of cookies.

Using PHP for HTTP authentication:

is a basic authentication mechanism that is created by combining the header () function and the Phpauthuser, PHP_AUTH_PW global variable method.

A simple PHP script can simulate an HTTP authentication request/Response system by sending the appropriate HTTP headers to automatically display the Username/Password dialog box on the client screen.

<?php   
if ((!isset ($PHP _auth_user)) | | (!isset ($PHP _auth_pw)   
) {   
header (' Www-authenticate:basic reallm= ' my Private Stuff ');   
Header (' http/1.0 401 Unauthorized ');   
Echo ' Authorization Required. '   
Exit;   
}   
else if ((Isset ($PHP _auth_uset)) && (Isset ($PHP _auth_pw))   
{   
if ($PHP _auth_user!= "Validname") | | ($PHP _auth_pw!= "Goodpassword"))   
{   
header (' Www-authenticate:basic reallm= ' my Private Stuff ');   
Header (' http/1.0 401 Unauthorized ');   
Echo ' Authorization Required. '   
Exit;   
}   
else if (($PHP _auth_use== "Validname") | | 

($PHP _auth_pw== "Goodpassword"))   
{   
echo ' you are authorized ';   
}   
}   
? >

Cookies are a way for a server to store information on a user's machine. In this way, the site can remember or track users during the visit.

Configuration and application of cookies:

Cookies are returned to the browser from the server side via HTTP headers, first, the server side uses the Set-cookie header in the response to create a cookie, and then the browser passes the cookie header in its request (containing this already created , and return to the server to complete the browser verification.

Setcookie (string name, string value, int expire,string path, string domain, int secure);

Where name is the cookie variable name identifier, you can use it as a generic variable name in PHP to reference the cookie variable. Value is the initial value of the cookie variable, expire represents the valid time for the cookie variable, path is the associated path for the cookie variable, and the Web site that represents the cookie variable in domain, and secure is valid for the secure transmission of HTTPS.

Receiving and processing cookies

PHP's support for cookies receiving and processing is very good, and is completely automatic, as is the principle of form variables, particularly simple.

For example, setting up a cookie,php named Mycookier will automatically parse it from the HTTP header received by the Web server and form a variable like a normal variable named MyCookie, which is the value of the cookie. Arrays are also applicable. Another option is to refer to the PHP global variable http_cookie_vars array.

Examples are as follows: (assuming that these are set in the previous page and still valid)

echo $MyCookie;   
echo $CookieArray [0];   
echo $_cookie["MyCookie"];   
echo $HTTP _cookie_vars["MyCookie"];

Delete Cookies

There are two ways to delete a cookie that already exists:

Setcookie ("Cookie", ""); Assign null value to it
Setcookie ("Cookie", "Value", Time () -1/time ());/Set Expiration

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.