Home  >  Article  >  Backend Development  >  Lecture 4 of "The Quickest to Understand PHP Programming": date, form reception, session, cookie_PHP Tutorial

Lecture 4 of "The Quickest to Understand PHP Programming": date, form reception, session, cookie_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:55743browse

Example 11: Date stamp, date display

Copy code The code is as follows:

echo time( );//Return a string of time stamps calculated in seconds
echo "
";
echo date("Y-m-d H:i:s",time()+8*3600);/ /Format time, +8*3600 becomes China time zone time
echo "
";
$str="2010-08-24 10:26:10";
echo date( "Y-m-d H:i:s",strtotime($str));//strtotime converts the string into a timestamp
echo "
";
?>

Example 12: Form variable encoding and reception
Copy code The code is as follows:

echo $str=urlencode("first page");//URL variable encoding, the same as google
echo "
".urldecode($str);//URL variable decoding, I saw it
echo "
First page";
echo "
";
if ($_GET)echo "Variable received:".$_GET['page'];//$_GET corresponds to $_POST, PHP automatically recognizes the URL encoding and decodes it automatically.
echo "
";
?>

Example 13: session usage
Copy code The code is as follows:

session_start();//session will expire immediately after the browser is closed. This declaration is needed to read and write the session
$_SESSION['id ']='server';//This is declaration and initialization, the same as array usage
$_SESSION['name']='session';
print_r($_SESSION);//session registration successful output An array
echo "
";
$_SESSION['id']='Still a server';//Change a session and then output it to see
unset($_SESSION['name' ]);//Log out a session and check the output again
print_r($_SESSION);
echo "
";
?>

Example 14 : Cookie usage
Copy code The code is as follows:

setcookie("id","Customer end");//This is the function that declares and initializes cookies. It will expire when you close the browser
setcookie("name","session",time()+3600);//It will expire after 1 hour from then on
print_r($_COOKIE);//session registration successfully outputs an array , you can also use $HTTP_COOKIE_VARS to access
echo "
";
setcookie("id", "Still client");//Change a session and then output it to see
unset($ _COOKIE['name']);//Log out a session and then output it, which is equivalent to setcookie("name","",time()-1); This is said in many books. In fact, unset can also be used Can log out
print_r($_COOKIE);
echo "
";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322477.htmlTechArticleExample 11: Date stamp, date display copy code The code is as follows: ?php echo time();//Return one A string of time numbers stamped in seconds echo "br"; echo date("Y-m-d H:i:s",time()+8*3600);//Format...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn