[Example] Cookies, Sessions and PHP - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: [Example] Cookies, Sessions and PHP (/showthread.php?tid=4190) Pages:
1
2
|
[Example] Cookies, Sessions and PHP - Gaijin - 01-05-2010 Here is a small example of what you can do with cookies and sessions. It checks if a user has a cookie created by your script, if there is no cookie the code prints out a HTML form asking user for a secret word. The user can submit a wrong word only 3 times, but in this code when s/hereaches thelimit, it is set back to o. Can you eat the cookie? No, ;) A cookie is small string that is stored on the users computer by your script. Cookies are used for tracking and transporting data from one page to other on your site. They can be used for saving the current user information such as, Username, logged in time and everything that can be placed in a string line. NOTE: If you already didn't know a user is nowdays able to decide if he wants to allow cookies or not, if s/he doesn't allow the creation of a cookie the code posted here will not work! For this case we have the SESSIONS. What are Sessions? A Session is just same as a Cookie, but they can't be blocked by the user! They are only temporary files, which are only existent while the user has s/hes browser open, and are deleted when the browser is closed. The script I show you here uses to store a Cookie, because if we would use a Session, the user would need to input the secret word everytime they restart the browser. PHP Code: <?php I will skip the first IF block, $_GET['eat'] and talk about later. We start off with start_session(), this function needs to be called before any headers were sent or any content was printed. Otherwise you'll get an FATAL ERROR. In the Line 12 the code checks if the user has submited the form, $_POST['secret'] (secret is the name of the Form submit button). Inside of this IF block, we first check if a Session named "attempts" was created, and define it as "1" if it wasn't created. Next we check if the Session value is over "3", in that case we delete it ( unset() ) and kill the script showing an error message. If the above check didn't kill the script because of too many attempts, we check the input of the user and if it matches our "secretkey". $_POST['key'] (key = Form Input field) If the user inputs "secretkey" we use setcookie() to store a cookie on his system, with the name "TUT_USER" and mp5 encoded ip as it's value. The cookie is set to expire after 1 hour, time()+3600 In the end we Welcome the user. NOTE: setcookie() must be same as start_session() run before any output to the site. If the user input isn't same as our "secretkey", we icrease the $_SESSION['attempts'] by 1 and kill the script showing the error message. Now the user has one attampt less. Right at the last IF block, we use isset() to check if a Cookie was set, $_COOKIE['TUT_USER'] (create by the avobe setcookie() function). If the user have a cookie from your domain and with the name "TUT_USER" we print out a message, otherwise we print the input form again! Now back at the very first IF block, $_GET['eat'], here we check if a user wants to delete a cookie, a link is printed if a cookie was created as you can see. And we use unset to delete the cookie. More to read..... http://www.php.net/manual/en/function.setcookie.php http://www.php.net/manual/en/function.session-start.php RE: [Example] Cookies, Sessions and PHP - flAmingw0rm - 01-08-2010 Great tutorial, thanks! RE: [Example] Cookies, Sessions and PHP - Gaijin - 01-08-2010 I'm glad you like it and also hope that it helped you somehow! RE: [Example] Cookies, Sessions and PHP - Spl3en - 01-14-2010 Very well written and helpful RE: [Example] Cookies, Sessions and PHP - Gaijin - 01-14-2010 Thanks Spl3en... I'm glad you like it! ;) RE: [Example] Cookies, Sessions and PHP - Peachyâ„¢ - 02-25-2010 Enjoy reading your tutorials, you manage to keep a focus point throughout, well planned and executed. Many people will learn from this, well done fella. RE: [Example] Cookies, Sessions and PHP - Saint Michael - 12-17-2010 Thanks for this, it was very helpful indeed. RE: [Example] Cookies, Sessions and PHP - Bauer - 12-19-2010 Thanks for this tutorial! It's really helpful! RE: [Example] Cookies, Sessions and PHP - Orgy - 12-20-2010 Wait, so I can't eat the cookie? RE: [Example] Cookies, Sessions and PHP - Proof - 12-21-2010 (12-20-2010, 07:18 AM)Orgy Wrote: Wait, so I can't eat the cookie? babe I thought I picked up enough tacos for the week... OT: Nice tutorial. |