Date, form receiving, session, cookie (php Tutorial 3)

Source: Internet
Author: User
Tags echo date md5 session id php tutorial setcookie

Session

 

The code is as follows: Copy code
<? Php
Session_start (); // The session will expire immediately after the browser is closed. You need to declare it to read and write the session.
$ _ Session ['id'] = 'server'; // This is the declaration and initialization, the same as the array usage.
$ _ Session ['name'] = 'session ';
Print_r ($ _ session); // An array is output after session registration is successful.
Echo "<br> ";
$ _ Session ['id'] = 'or server'; // change a session and output it.
Unset ($ _ session ['name']); // cancel a session and output it.
Print_r ($ _ session );
Echo "<br> ";
?>

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 a user request (explicitly called by session_start () or secretly called by session_register () checks whether a specific session id is sent in the request. If yes, the previously saved environment will be rebuilt.

Page1.php

The code is as follows: Copy code
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 ();


Page2.php

The code is as follows: Copy code
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. The information sent from a form with the get method is visible to anyone (displayed in the address bar of the browser), and the amount of information sent is limited (up to 100 characters ).

Example

The code is as follows: Copy code
<Form action = "welcome. php" method = "get">
Name: <input type = "text" name = "name"/>
Age: <input type = "text" name = "age"/>
<Input type = "submit"/>


</Form> when a user clicks the submit button, the sent url is similar to the following:

Http://www.111cn.net/welcome.php? Name = peter & age = 37 "welcome. php "files can now get form data through 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: Copy code
Welcome <? Php echo $ _ get ["name"];?>. <Br/>
You are <? Php echo $ _ get ["age"];?> Years old!

Let's look at a simple and detailed instance.

The code is as follows: Copy code
<? Php
Echo $ str = urlencode ("first page"); // url variable encoding, same as google
Echo "<br>". urldecode ($ str); // url variable decoding, I see
Echo "<br> <a href = index. php? Page = ". $ str."> page 1 </a> ";
Echo "<br> ";
If ($ _ get) echo "the variable has been received :". $ _ get ['Page']; // $ _ get corresponds to $ _ post. php automatically recognizes url encoding and decodes it.
Echo "<br> ";
?>

Cookie

Php cookie usage
$ Time = time () + 300; // expire in 5 minutes
$ Code = md5 ($ string. $ time. $ salt );
Setcookie ('check _ time', $ time );
Setcookie ('code', $ code );

// Verification
$ Time = time ();
If ($ check_time <$ time) // if the server time is longer than the verification time, it is an expiration date.
// Expire expired
   
If (md5 ($ string. $ check_time. $ salt )! = $ Code)
// Error
In fact, this method is relatively better, because it cannot be submitted after expiration, but it can still be submitted without limit before expiration.

View cookie instances

 

The code is as follows: Copy code
<? Php
Setcookie ("id", "client"); // This is the declaration and cookie initialization function. The browser is disabled.
Setcookie ("name", "session", time () + 3600); // expires one hour later
Print_r ($ _ cookie); // An array is output after the session registration is successful. You can also access it using $ http_cookie_vars.
Echo "<br> ";
Setcookie ("id", "or client"); // change a session and output it.
Unset ($ _ cookie ['name']); // cancel a session and output it. This is equivalent to setcookie ("name", "", time ()-1 ); this is what many books have said. In fact, unset can also be used to log out.
Print_r ($ _ cookie );
Echo "<br> ";
?>

Date

 

The code is as follows: Copy code
<? Php
Echo time (); // returns a string of timestamp in seconds.
Echo "<br> ";
Echo date ("y-m-d h: I: s", time () + 8*3600); // format time, + 8*3600 to China time zone
Echo "<br> ";
$ Str = "10:26:10 ";
Echo date ("y-m-d h: I: s", strtotime ($ str); // Convert string to timestamp
Echo "<br> ";
?>

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.