Problem with PHP cookies - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Programming
Click Here to Login
Closed Thread
 
Thread Tools Display Modes
 
Old 04-29-2009, 02:14 AM   #1 (permalink)
Web Programmer
 
murdocsvan's Avatar
 
Join Date: Jun 2007
Location: Surrey, UK
Posts: 896
Default Problem with PHP cookies

For some reason im having a great deal of problems with using cookies on my website. I have a login system which i initially set to use sessions, but decided to use cookies as sessions was a bit glitchy. The order of my website goes:

Login form > Check Login Script > Login Success Script > index.htm

on the first script, this is code that sets the cookie:

PHP Code:
        // Register $myusername, $mypassword and redirect to file "login_success.php"
        
$expire=time()+60*60*24*5;
        
setcookie('user'$username$expire);
        
header("location:login_success.php"); 
And this is the piece of code on each page that checks that the cookie is there:

PHP Code:
<?php

if(!isset($_COOKIE['user']))
    {
    
header("location:login/main_login.htm");
    }

?>

Now for some reason, it will go to the Login Success page, and not have a problem even though that page has the cookie checking code on it. However, when the Login Success page then redirects to the index.htm page, it goes straight back to the login form, thinking the cookie's not there. The second bit of code is identical on all the pages i have protected by the login system.

Thanks in advance.
__________________

__________________
PHP + MYSQL, ASP.Net + C# with SQL, WCF, Python, XHTML + CSS, HTML5, JavaScript, jQuery, Flash + ActionScript 3, XML, SVG.

Web Developer Portfolio
murdocsvan is offline  
Old 04-29-2009, 02:26 PM   #2 (permalink)
Wizard Techie
 
CrazeD's Avatar
 
Join Date: Feb 2006
Location: Maine
Posts: 3,736
Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Problem with PHP cookies

Try the Cookie Editor plugin for Firefox to make sure the cookie is actually being set, and with the right parameters.
__________________

__________________

Intel Q9400 | Gigabyte EP45-UD3P | G.Skill 2x2GB DDR2 1000 | XFX HD5870

Need website help? PM me!
CrazeD is offline  
Old 05-25-2009, 06:53 PM   #3 (permalink)
Ultra Techie
 
Join Date: Sep 2005
Posts: 651
Send a message via AIM to tommyboy123x
Default Re: Problem with PHP cookies

Its probably because on every page you MUST use
session_start();
even when you are not using the sessions / variables on that page. Otherwise they are lost.

Make sure session_start(); is on top of each page BEFORE any output is made
__________________

Dollar Hauler [dot] Com :)
tommyboy123x is offline  
Old 05-25-2009, 07:42 PM   #4 (permalink)
Wizard Techie
 
CrazeD's Avatar
 
Join Date: Feb 2006
Location: Maine
Posts: 3,736
Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Problem with PHP cookies

He's using cookies, not sessions.
__________________

Intel Q9400 | Gigabyte EP45-UD3P | G.Skill 2x2GB DDR2 1000 | XFX HD5870

Need website help? PM me!
CrazeD is offline  
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard to diagnose internet problem theorigamist Computer Networking and Internet Hardware 2 02-17-2009 11:32 PM
Problem with 3D graphics mordi05 Microsoft Windows and Software 6 12-10-2007 10:37 PM
Serious computer problem. HeeRoMaKi Hardware Repairs and Troubleshooting 71 07-28-2007 10:42 PM
Dhcp leasing problem Kloppstock Computer Networking and Internet Hardware 12 05-13-2007 06:13 PM
cant find the problem... Jhill1 Hardware Repairs and Troubleshooting 13 05-07-2007 03:51 PM



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 02:44 PM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2018, vBulletin Solutions, Inc.