Getting Started with PHP (String, cookie,session)

Source: Internet
Author: User
Tags getting started with php setcookie urlencode

Get Started with PHP (String, cookie,session), a friend of the need can refer to below.


String

Gets the length of the string: strlen () function
Get Chinese text long echo mb_strlen ($str, "UTF8");

English string interception

    1. $str = ' I love you ';
Copy Code

Capture the letters of love
Echo substr ($str, 2, 4);//Why the starting position is 2, because the SUBSTR function calculates that the string position is starting from 0, that is, the position of 0 is the space where the i,1 is, and the position of L is 2. Starting from position 2, take 4 characters, that is love
Chinese string interception
Mb_substr ();

String Lookup
Strpos (string to be processed, string to locate, start position of anchor [optional]) Replacement string
Str_replace (The string to find, the string to replace, the string to be searched, and the replacement to count [optional])

formatting strings

    1. $STR = ' 99.9 ';
Copy Code
    1. $result = sprintf ('%01.2f ', $str);
Copy Code

echo $result;//Results show 99.90

Merging strings

    1. $arr = Array (' Hello ', ' world! ');
    2. $result = Implode (' ', $arr);
    3. Print_r ($result);//results show Hello world!
Copy Code

Split string

    1. ' $str ' = ' apple,banana ';
    2. ' $result ' = explode (', ', $str);
    3. Print_r ($result);//Results Display array (' Apple ', ' banana ')
Copy Code

String escape function Addslashes ()
Function Description: Used to add an escape character to a special character, returns a string
Return value: An escaped string
Example:

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

Cookies

Common parameters
Name (COOKIE name) can be accessed by $_cookie[' name ')
Value (values of the cookie)
Expire (expiry time) UNIX timestamp format, default = 0, indicates that the browser is off and is invalidated
Path (valid path) if the path is set to '/', the entire site is valid
Domain (valid domain) default entire domain name is valid, if ' www.imooc.com ' is set, it is only valid in WWW subdomain
2.
PHP also has a function to set the cookie Setrawcookie,setrawcookie is basically the same as Setcookie, the only difference is that value values are not automatically urlencode, So when you need to manually do UrlEncode delete and set the expiration time
Setcookie (' Test ', ", Time ()-1); Valid path
Setcookie (' Test ', Time (), 0, '/path ');//Set the path and its sub-directory valid session
Using the session in PHP is very simple, first executing the Session_Start method to open the session, and then through the global variable $_session session read and write.

Session_Start ();
$_session[' Test ' = time ();
Var_dump ($_session);
The session automatically encode and decode the values to be set, so the session can support any data type, including data and objects. Delete
Deleting a session value can use PHP's unset function, which is removed from the global variable $_session and cannot be accessed

Session_Start ();
$_session[' name '] = ' jobs ';
unset ($_session[' name ');
echo $_session[' name ']; Hint 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 delete all data, but session_id still exists
Session_destroy does not immediately destroy the value in the global variable $_session, and the $_session is empty only the next time it is accessed, so you can use the unset function if you need to destroy $_session immediately.
If it is necessary to destroy the session_id in the cookie at the same time, usually when the user exits, you also need to explicitly call the Setcookie method to delete the session_id cookie value.
PHP, cookies, session
  • Related Article

    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.