80 likes | 193 Vues
This advanced PHP introduction, presented by Chin-Chang Chang on April 23, 2007, covers the basics of using cookies and sessions in PHP. The session provides practical examples of how to set and retrieve cookies, manage session data, and clear cookies with specific paths. Attendees will learn about the `setcookie()` function, session management techniques, and the role of HTTP cookie variables in PHP. This session is essential for those looking to enhance their web applications with persistent data storage.
E N D
PHP-- advanced introduction Speaker: Chin-Chang Chang Date:2007.4.23
Cookie(1/2) • setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] ) • Ex. setcookie('stu_id', '95321517'); • $HTTP_COOKIE_VARS['cookie'] • echo $HTTP_COOKIE_VARS['stu_id'];
Cookie(2/2) <?php setcookie('tCookie', $name, time()+60*60*24); echo "cookie = " . $HTTP_COOKIE_VARS['tCookie']; ?> <html> <head><title>cookie</title></head> <body> <form action="<?= $PHP_SELF; ?>" method="POST"> <input type="text" name="name"><input type="submit"> </form> </body> </html> http://stu.csie.ncnu.edu.tw/~beautidays.99/cookie.php
Cookie Path <?php setcookie('path_test', $test, time()+60*60*24, '/~beautidays.99/cookie'); echo "cookie = " . $HTTP_COOKIE_VARS['path_test']; ?> http://stu.csie.ncnu.edu.tw/~beautidays.99/cookie/cookie2.php http://stu.csie.ncnu.edu.tw/~beautidays.99/cookie/path_test.php http://stu.csie.ncnu.edu.tw/~beautidays.99/path_test.php
Cookie Clear • setcookie('path_test', '', time()+60*60*24); • setcookie('path_test', '', time()+60*60*24, '/~beautidays.99/cookie'); http://stu.csie.ncnu.edu.tw/~beautidays.99/link.php
Session(1/2) • <?php • session_start(); • if(($HTTP_POST_VARS['user']==andy)&&($HTTP_POST_VARS['pass']==123)) • { • echo "success!"; • $user=$HTTP_POST_VARS['user']; • session_register('user'); • echo '<a href=“protected.php">next</a>'; • } • else • header('Location:login.php?error=1'); • ?> • http://stu.csie.ncnu.edu.tw/~beautidays.99/login2.php
Session(2/2) <?php include('auth2.php'); ?> success! <form action="login2.php" method="POST"> <input type="submit" value="logout" onclick="<? session_unregister('user'); ?>"> </form>
Reference • http://chensh.loxa.edu.tw/php/