PHP programming fastest understanding Lecture 4: Date, form receiving, session, cookie_php basics

Source: Internet
Author: User
Tags echo date
PHP programming is the fastest way to understand. Lecture 4: Date, form receiving, session, and cookie. For more information, see. Instance 11: displays the date stamp and date

The code is as follows:


Echo time (); // returns a string of timestamp in seconds.
Echo"
";
Echo date ("Y-m-d H: I: s", time () + 8*3600); // format time, + 8*3600 to China time zone
Echo"
";
$ Str = "10:26:10 ";
Echo date ("Y-m-d H: I: s", strtotime ($ str); // Convert string to timestamp
Echo"
";
?>


Example 12: form variable encoding and receiving

The code is as follows:


Echo $ str = urlencode ("first page"); // URL variable encoding, same as google
Echo"
". Urldecode ($ str); // URL variable decoding, I see
Echo"
Page 1 ";
Echo"
";
If ($ _ GET) echo "the variable has been received :". $ _ GET ['Page']; // $ _ GET corresponds to $ _ POST. PHP automatically recognizes URL encoding and decodes it.
Echo"
";
?>


Instance 13: session usage

The code is as follows:


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"
";
$ _ SESSION ['id'] = 'or server'; // change a SESSION and output it.
Unset ($ _ SESSION ['name']); // cancel a SESSION and output it.
Print_r ($ _ SESSION );
Echo"
";
?>


Instance 14: cookie usage

The code is as follows:


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"
";
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"
";
?>

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.