0 Users appreciate this thread.
[PHP] Check if Session or Cookie is Set
CoolApps (2014-06-04 14:39:19)This is a web sub-forum after all, so people could save their time searching the interwebs.
Yes, this is basic, but this is just an example of how you would use it in a IF statement, nothing more, nothing less.
Expect it to be easy, it's for the first timers.
Newer account: NodePoint
Log in to submit a comment
Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)
New registered users today: 7
Newest registered user: ElegantVulpes
The code below is the base of how this will work.
If you're going to use cookies, you MUST sanitize the cookie output using functions such as mysqli_real_escape_string() and stripslashes(), anyway, this isn't a guide showing you how to avoid SQL Injections and such so moving on...
CODE TO BE EXECUTED
exit(); // This cuts off the rest of the script
}
As you can see, we're using a IF statement and a global variable ($_) to do the job.
What this basically does is check if the cookie assigned to your site called "username" doesn't exist (thanks to "!" meaning false), if the cookie doesn't exist then it'll execute the code within.
As stated in the PHP comment next to "exit();", it cuts off the rest of the script below, so if there is a vital part of a PHP script which needs executing regardless, place it above this script!
Another PHP tutorial by me.
2014-06-03 00:41:07