homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

New to PHP/mySQL - Cookies question

 5:33 pm on Oct 31, 2002 (gmt 0)

I have been learning PHP and mySQL for about a week. I just made a login page with a simple form on it. It then goes to a php page that validates if the login and password are in the database then sets a cookie for the username and login an redirects to the main application page like this:

setcookie("ck_user", $user);
setcookie("ck_pass", $pass);
header("Location: /main.php");

This cookie will only be valid for the session since I didn't set a time on it, correct?

Now on main.php, I want it to check for the cookies and if they are not there I want to redirect them to the login page, and if the cookie is there I want it to execute the main.php page. How do I go about doing this?

Thanks a bunch!



 9:27 pm on Oct 31, 2002 (gmt 0)

setcookie from php.net

One thing that comes to mind first is you are setting two cookies for one site, bad plan. I have had a lot of trouble when people have used this method. You are better off setting one single cookie with all the info in it.

Take a look at serialize()

this will provide you a way to put multiple values in there.

here is a little auth lib I used somewhere at sometime, it was a while ago but it worked well. I also didn't write this particular one, someone who worked for me did.


function auth_login($username) {
if (!empty($username)) {
$out_auth = serialize(array($username,md5($username.AUTH_HASH)));
setcookie('auth', $out_auth, 0, '/', '', 0);

function auth_verify() {
global $auth, $username;
list($username, $auth) = unserialize($auth);
$test_auth = md5($username.AUTH_HASH);
if ($auth == $test_auth)
return 1;
return 0;

function auth_logout() {
setcookie('auth', '', -1, '/', '', 0);


 9:10 pm on Nov 1, 2002 (gmt 0)

Thanks for the help! Looks like I have some reading to do... like I said I am very new to this.

Thanks Again! :)


 3:12 pm on Nov 28, 2002 (gmt 0)

If your doing a login system, take a look at PHP Sessions. These make life easy because PHP deals with whether or not the browsers can accept cookies and storing persistant data.



 1:50 pm on Nov 29, 2002 (gmt 0)

Back to your main question about redirecting to a login page if the cookies aren't set, just use something like:

if (!$HTTP_COOKIE_VARS['ck_user']) header("Location: login.php");

This is assuming your login cookie is called 'ck_user'


 7:14 pm on Dec 3, 2002 (gmt 0)

I am just now getting a Login / Authentification system together. It has Sessions, when Cookies are not available, mailback verification, and mySQL support.
I want to add a pages viewed history for each user, multiple email support, automated email ping to see if their email is still valid, and email their OTHER emails.

Does anyone know if others already have these features rolled into one package beyond what PHPBuilder supplies?


 7:16 pm on Dec 3, 2002 (gmt 0)

Have you looked into the scripts at Hotscripts.com?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved