PHP Getting Started: session control skills (cookie and session); getting started with cookie

Source: Internet
Author: User
Tags php session

PHP Getting Started: session control skills (cookie and session); getting started with cookie

This article describes the PHP session control skills. We will share this with you for your reference. The details are as follows:

Demo1.php

<Form method = "get" action = "Demo2.php"> Name: <input type = "text" name = "username"/> <br/> <input type = "submit" value = "submit"/> </form>

Demo2.php

<? Php // echo $ _ POST ['username']; // if the form adopts get transmission, echo $ _ GET ['username'] must be accepted; // What is better .???? // $ _ POST ['']; is it safer?>

Demo3.php

<? Php // create a Cookie // Cookie is a small file stored on your client, this file contains your login information // setcookie can create a client's cookie file // The first parameter indicates the cookie name, the second parameter indicates the cookie name value // when the session ends, that is, when your browser is closed, it will disappear, automatically delete // create a cookie with an expiration date. The expiration time is based on the current timestamp + seconds. // time () + (7*24*60*60) indicates that the setcookie will change in the next 7 days. Once the browser is refreshed, the old cookie will overwrite the setcookie ('name', 'onestopweb', time () + (7*24*60*60);?>

Demo4.php

<? Php setcookie ('name', 'onestopweb'); // read the local cookie, using a super global variable $ _ COOKIE // put the cookie name in it. // There is a feature, setcookie is not generated in a timely manner. It will take a slow shot // PS: the first time it is refreshed, it will only overwrite the original generation. // But it is still obtained, and the second Refresh can be truly obtained. // Echo $ _ COOKIE ['name']; // use the variable detection function to determine whether the cookie exists if (isset ($ _ COOKIE ['name']). {echo $ _ COOKIE ['name'];} else {echo 'this user does not exist';}?>

Demo5.php

<? Php // Delete the cookie setcookie ('name', 'onestopweb '); // The cookie is deleted in the middle. // set this value to null. // setcookie ('name ',''); // when I adjust the expiration time to the current one second, the setcookie ('name', 'onestopweb', time ()-1) will expire ); echo $ _ COOKIE ['name'];?>

Demo6.php

<Form method = "post" action = "Demo7.php"> Name: <input type = "text" name = "username"/> <br/> <input type = "submit" value = "submit"/> </form>

Demo7.php

<? Php // if the specified name is the same, a cookie is generated // if (isset ($ _ POST ['username']) & $ _ POST ['username'] = 'onestopweb') {// if it is correct, I generate a cookie and redirect to setcookie ('name ', 'web'); header ('location: Demo8.php ');} else {header ('location: Demo6.php');}?>

Demo8.php

<? Php if (isset ($ _ COOKIE ['name']) {echo 'Welcome :'. $ _ COOKIE ['name'];} else {echo 'illegal logon';}?>

Demo9.php

<? Php session_start (); // account session processing // as long as the session is used, you must enable session_start () // at the beginning of the file // create a session, directly assign a value using the super global variable. // The session is stored on the server, which is generally stored for 1440 seconds. // If the webpage does not have any operation, it will be automatically destroyed. Of course, you can use php. ini to modify the Save time // If the browser is closed, it will be destroyed automatically. // Timeliness. It is not as slow as cookie. $ _ SESSION ['name1'] = 'onestopweb'; $ _ SESSION ['name2'] = 'onestopweb '; // echo $ _ SESSION ['name']; // if (isset ($ _ SESSION ['name']) {// echo $ _ SESSION ['name']; //} else {// echo 'does not exist. '; //} // The method is not deleted. // $ _ SESSION ['name'] = ''; // real deletion method // unset ($ _ SESSION ['name']); // if (isset ($ _ SESSION ['name']) {// echo $ _ SESSION ['name']; //} else {// echo 'does not exist. '; //}?>

Demo10.php

<? Php session_start (); // destroy all sessions, and destroy session_destroy (); echo $ _ session ['name1']; echo $ _ SESSION ['name2']; // cookie applies to member login, shopping cart... // Because it does not occupy server resources, there are many Members and a large number of shopping carts. Therefore, the cookie // session is generally used for background management and logon, with fewer users // security, will it automatically expire after a period of time?>

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.