PHP---No login within one week

Source: Internet
Author: User
Tags http cookie setcookie

In many landing interface will have a week-free function, the principle of implementation is, the option to login-free in front of the Web page as a checkbox, after the user select a post to send to the server, the server through Setcookie this function, set the client cookie variables, effective time.

Definitions and Usage

The Setcookie () function sends an HTTP cookie to the client.

A cookie is a variable that is sent by the server to the browser. Cookies are usually small text files that the server embeds into the user's computer. This cookie is sent whenever the computer requests a page through the browser.

The name of the cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created, containing the value of the cookie.

The cookie must be assigned before any other output is sent. If successful, the function returns True, otherwise it returns false


<?php
$value = "my cookie value";

Send a cookie
setcookie ("TestCookie", $value, Time () +3600*24) that expired when you were 24.
>

Different ways to retrieve cookie values:

 

Output:

My cookie value i
cookie value
Array ([TestCookie] => my cookie value)
Example 3

Delete a cookie by setting the expiration date to the past date/time:

<?php
///Set the expiration date to
Setcookie ("TestCookie", "", Time ()-3600).
>

Example 4

Create an array cookie:

<?php
Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");

Output cookie (after overloaded page)
if (isset ($_cookie["Cookie"))
  {
  foreach ($_cookie["Cookie"] as $name => $value) c7/>{
    echo "$name: $value <br/>";
    }
? >

Output:

Three:cookiethree
two:cookietwo
one:cookieone

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.