Session and data hold

Source: Internet
Author: User
1 Maintaining user information in the site through SessionID


    
     session_start();$_SESSION['visits']++;print'You have visited here '.$_SESSION['visits'].' times.
';echo'session id = '.$_COOKIE['PHPSESSID'];echo"
";echo"session name = ".session_name()."
";?>

SessionID recorded in the global variable _cookie, SessionID's name is Phpsessid,phpsessid can also be obtained through Session_name ().

2 Prevention of Session hijacking

    !--? php  ini_set ( ' sessio.use_only_ Cookies ' ,  true ); Session_Start ();  $salt  =  ' Yourspecialvaluehere ' ;  $tokenstr  = date ( ' W ' ).  $salt ;  $token  = MD5 ( $tokenstr );  Echo   ' token = ' .  $token .  ' 
' ; if (! isset ( $_request [ ' token ' ]) | | $_request [ token ' ]! = $token ) { exit ;} $_session [ ' token ' ] = $token ; Output_add_rewrite_var ( ' token ' , $token ); Echo ' link ' ; Ob_flush (); Output_reset_rewrite_vars (); ?

     
      session_start();output_add_rewrite_var('var', 'value');echo'link';ob_flush();output_reset_rewrite_vars();echo'link';?>以上例程会输出:<ahref="file.php?PHPSESSID=xxx&var=value">link
     
      a><ahref="file.php">link
    
     a>

3 Prevention Session Customization

    • The session identifier is not appended to the session cookie on the URL.
    • Frequent generation of new SessionID

    
     ini_set('session.use_only_cookie', true);session_start();if(!isset($_SESSION['generated']) || $_SESSION['generated'] < (time() - 30)){    session_regenerate_id();    $_SESSION['generated'] = time();}echo$_COOKIE['PHPSESSID']

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes a session and data retention, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.