Date, form receiving, session, cookie

Source: Internet
Author: User
This tutorial mainly describes how to apply and use date, form receiving, session, and cookie in php. The following gives an example to illustrate these tutorials in php. session: PHP instance code: l... this tutorial mainly describes how to apply and use date, form receiving, session, and cookie in php. The following gives an example to illustrate these tutorials in php.

Session:The PHP instance code is as follows:

 "; $ _ Session ['id'] = 'or server'; // change a session, and then output the unset ($ _ session ['name']); // log out of a session, and then output the print_r ($ _ session); echo"
";?>

Session support allows users to register any number of variables and keep them for use by each request. when visitors visit the website, php will automatically (if session. auto_start is set to 1) or when the user requests (explicitly called by session_start () or session_register (), check whether a specific session id is sent in the request. If yes, the previously saved environment is rebuilt.

The page1.php code is as follows:

Session_start (); echo 'Welcome to page # 1';/* Create a session variable and assign a value to the session variable */$ _ session ['favcolor'] = 'green '; $ _ session ['Animal '] = 'cat'; $ _ session ['Time'] = time ();

The page2.php code is as follows:

Session_start (); print $ _ session ['Animal ']; // print a single session var_dump ($ _ session); // print the session value passed by page1.php.

Get:$ _ Get variable

The $ _ get variable is an array with the variable name and value sent by the http get method.

The $ _ get variable is used to collect the values in the form from method = "get" and send information from the form with the get method, it is visible to anyone (displayed in the address bar of the browser) and limited to the amount of information sent (up to 100 characters ).

The PHP instance code is as follows:

 

When you click the submit button, the sent url is similar to the following:

Http://www.phprm.com/welcome.php? Name = peter & age = 37 "welcome. php "files can now get form data using the $ _ get variable (note that the name of the form field will automatically become the id key in the $ _ get array). The code is as follows:

welcome 
  .
you are years old!

Let's look at a simple and detailed example. the code is as follows:

 ". Urldecode ($ str); // url variable decoding, I can see echo"
Page 1 "; echo"
"; If ($ _ get) echo" variable received :". $ _ get ['Page']; // $ _ get corresponds to $ _ post. php automatically recognizes url encoding and decodes it. Echo"
";?>

Cookie:Php cookie usage

 

Next, let's look at the cookie instance. the code is as follows:

 "; Setcookie (" id "," or client "); // change a session, and then output the unset ($ _ cookie ['name']); // cancel a session and output it again. it is equivalent to setcookie ("name", "", time ()-1, in fact, print_r ($ _ cookie) can also be deregistered with unset; echo"
";?>

Date:The code is as follows:

 "; Echo date (" y-m-d h: I: s ", time () + 8*3600); // format the time, + 8*3600 to China time zone echo"
"; $ Str =" 2010-08-24 10:26:10 "; echo date (" y-m-d h: I: s ", strtotime ($ str )); // Convert strtotime string into timestamp echo"
";?>


Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.