Php entry string, cookie, session

Source: Internet
Author: User
: This article mainly introduces php entry strings, cookies, and Sessions. For more information about PHP tutorials, see.
  1. String

    1. Obtain the string length: strlen () function
      Obtain the text length echo mb_strlen ($ str, "UTF8 ");
    2. English string truncation

      $str='i love you';

      // Intercept the letters love.
      Echo substr ($ str, 2, 4); // why is the start position 2? because the position of the string calculated by the substr function starts from 0, that is, the position 0 is I, the position of 1 is a space, and the position of l is 2. Start from position 2 and take 4 characters, that is, love.
      Chinese string truncation
      Mb_substr ();

    3. String search
      Strpos (the string to be processed, the string to be located, and the starting position to be located [optional])
    4. Replace string
      Str_replace (string to be searched, string to be replaced, string to be searched, replacement for counting [optional])
    5. Format a string

      $str = '99.9';
      $result = sprintf('%01.2f', $str);

      Echo $ result; // The result is 99.90.

    6. Merge strings

      $ Arr = array ('hello', 'World! '); $ Result = implode ('', $ arr); print_r ($ result); // The result shows Hello World!
    7. Split string

      '$ Str' = 'Apple, banana'; '$ result' = explode (', ', $ str); print_r ($ result ); // The result shows array ('apple', 'banana ')
    8. String escape function addslashes ()
      Function description: usedSpecial charactersReturns a string with an escape character.
      Returned value: an escaped string.
      Example:

      $str= "What's your name ?";
      Echo addslashes ($ str); // output: what \'s your name

  2. Cookie

    1. Common parameters
      Name (Cookie name) can be accessed through $ _ COOKIE ['name ']
      Value (Cookie value)
      Expire (Expiration Time) Unix timestamp format. the default value is 0, indicating that the browser is disabled or becomes invalid.
      Path (valid path) if the path is set to '/', the entire website is valid.
      Domain (valid domain) is valid for the entire domain name by default. if 'www .imooc.com 'is set, it is valid only in the www subdomain.
      2.
      There is also a function setrawcookie in PHP to set cookies. setrawcookie is basically the same as setcookie. The only difference is that the value does not automatically perform urlencode. Therefore, you need to manually perform urlencode when necessary.
    2. Delete and set expiration time
      Setcookie ('test', ", time ()-1 );
    3. Valid Path
      Setcookie ('test', time (), 0, '/path'); // you can specify the path and its subdirectories.
  3. Session
    1. It is very easy to use session in PHP. first execute the session_start method to start the session, and then useGlobal variables$ _ SESSION reads and writes sessions.

      session_start();
      $_SESSION['test'] = time();
      var_dump($_SESSION);
    2. The session automatically performs encode and decode on the value to be set. Therefore, the session can support any data type, including data and objects.
    3. Delete
      You can use the PHP unset function to delete a session value.Global variables$ _ Removed from the SESSION and cannot be accessed

      Session_start ();
      $ _ SESSION ['name'] = 'job ';
      Unset ($ _ SESSION ['name']);
      Echo $ _ SESSION ['name']; // the system prompts that the name does not exist.

      If you want to delete all sessions, you can use the session_destroy function to destroy the current session. session_destroy deletes all data, but session_id still exists.
      Session_destroy will not be destroyed immediatelyGlobal variables$_SESSIONValue in, only when the next visit,$_SESSIONTherefore, if you need to destroy $ _ SESSION immediately, you can use the unset function.
      If you need to destroy session_id in the cookie at the same time, it is usually used when the user exits, you also need to explicitly call the setcookie method to delete the cookie value of session_id

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above introduces the php entry string, cookie, session, including special characters, global variables, and hope to help those who are interested in the PHP Tutorial.

    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.