PHP Introductory Tutorial Session control tips (cookies and sessions) _php tips

Source: Internet
Author: User
Tags current time php session sessions setcookie mysql database

This example describes the PHP session control techniques. Share to everyone for your reference, specific as follows:

demo1.php

<form method= "Get" action= "demo2.php" >
  name: <input type= "text" name= "username"/> <br/>
  < Input type= "Submit" value= "submitted"/>
</form>

demo2.php

<?php
  //echo $_post[' username '];
  If the form takes a get transmission, then the acceptance must take the
  echo $_get[' username '];
  What kind of better.????
  //$_post['];  More secure
?>

demo3.php

<?php
  //Create a cookie
  //cookie is to save a small file in your client file that contains the information you log on
  //setcookie can create a client's cookie file
  / The first parameter represents the name of the cookie, and the second parameter represents the value of the cookie name
  //The so-called end of the session, when you have this browser closed, there is no, automatically delete
  //Create a containing expired cookies, The expiration time takes the current timestamp + seconds
  //time () + (7*24*60*60) indicates that the next 7 days
  //Once Setcookie has changed, a refreshing browser will overwrite the old cookie
  Setcookie ( ' Name ', ' Onestopweb ', Time () + (7*24*60*60));
? >

demo4.php

<?php
  Setcookie (' name ', ' Onestopweb ');
  Read the cookie for this machine, use a super global variable $_cookie
  //Inside put cookie name can
  //have a feature, Setcookie is not generated in time, it will slow a beat
  //ps: Slow A beat, the first time refresh, Just the build covers the original.
  //But get the previous one, and the second refresh can be really acquired.
  //echo $_cookie[' name '];
  Use the variable detection function to determine if the cookie exists
  if (isset ($_cookie[' name ')) {
    echo $_cookie[' name '];
  } else{
    Echo ' does not exist for this user ';
  }
? >

demo5.php

<?php
  //Delete cookie
  setcookie (' name ', ' Onestopweb ');
  Remove this cookie in the middle
  //Set this value to NULL to
  //setcookie (' name ', ');
  I adjust the expiration time to the current time less than a second, then is equal to the expiration of the
  Setcookie (' name ', ' Onestopweb ', Times ()-1);
  echo $_cookie[' name '];
? >

demo6.php

<form method= "POST" action= "demo7.php" >
  name: <input type= "text" name= "username"
  /> <br/> <input type= "Submit" value= "submitted"/>
</form>

demo7.php

<?php
  //If the name specifies the same name, then generate a cookie
  //Complete login
  if (isset ($_post[' username ')) && $_post[' Username ']== ' onestopweb ') {
    //If correct, I generate a cookie and then jump
    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 login ';
  }
? >

demo9.php

<?php
  session_start ()//Account opening session processing
  //session as long as you use this, you must open the Session_Start ()
  //Put the file at the beginning
  //Create Session, directly using the Super global variable assignment can
  //session is the existence of the server side, generally stored for 1440 seconds,
  //If the Web page does not have any operation, will automatically destroy, of course, can be modified by php.ini to save time
  // If the browser is turned off, it is automatically destroyed as well.
  //timeliness, unlike cookies will slow half beat
  $_session[' name1 ' = ' onestopweb ';
  $_session[' name2 '] = ' onestopweb ';
  echo $_session[' name '];
if (isset ($_session[' name ')) {
//   echo $_session[' name '];
}else{
//   echo ' does not exist for this person. ';
//
  not deleted method//
$_session[' name ' = ';
  True deletion method
  //unset ($_session[' name '));
if (isset ($_session[' name ')) {
//   echo $_session[' name '];
}else{
//   echo ' does not exist for this person. ';
}
?>

demo10.php

<?php
  session_start ();
  Destroy all sessions, destroy also slow half beat
  Session_destroy ();
  echo $_session[' name1 '];
  echo $_session[' name2 '];
  Cookies apply to member login, Shopping cart ah ...
  //Because he does not occupy server resources, so members are particularly many, shopping carts are particularly many, use cookies
  //session generally used for background management login, people less
  //security, a period of time does not operate automatically expired
?>

More interested in PHP related content readers can view the site topics: "PHP Cookie Usage Summary", "PHP Array" operation Techniques Daquan, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introduction Tutorial" , "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.