setcookie() not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

setcookie() not working

Post by guest »

Code: Select all

<?php
session_start();
?>
<?php
if ($_REQUEST&#1111;"loggedin"] == "t") &#123;s
setcookie("CG_login",$_REQUEST&#1111;"username"],time()*60*60*24*365);
echo "OK -".$_REQUEST&#1111;"username"];
&#125;
if ($_COOKIE&#1111;"CG_login"] && $_REQUEST&#1111;"loggedin"] != "t") &#123;
header("Location:Login/LoginSuccess.php?Username=".$_COOKIE&#1111;"CG_login"]);
&#125;
else &#123;

// OTHER CODE NOT SHOWN HERE

&#125;
When I Send The Required Details Using The Query String - To Set The Cookie, It Says It Has Done It.
Yet When I Reload The Page, It Goes Straight Into The ELSE Bit

Why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your second part of the if expression requires $_REQUEST['loggedin'] to not equal 't' .. if you just reload the page, loggedin will still be 't' (a requirement of your setcookie() bit.
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

Post by guest »

Reload -

Close The Window.
Open A New Window.
Type In The Adress (localhost/codeandgames/index2.php)
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

Post by guest »

PLEASE! THIS IS IMPORTANT AS I COMPLETELY TRANSFORMING MY SITE FROM PHP TO MYSQL
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yelling gets you nowhere. :roll:
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Have you tried printing out any variables for debugging purposes?
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

Post by guest »

Yes.

When I Get Home (At A Different Computer Now), I Will Upload A ZIP File.
Anyone With Apache + MYSQL + PHP5 - Unzip This Into A Directory Of Your Choice, And Run The index2.php in your browser.

Inspect The Code A Bit, You Will Soon See My Problem. It Is In index2.php and Login/LoginSuccess.php





ZIP WILL BE POSTED AT APPROXIMATELY 4:45 PM GMT
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

You should write out the process in steps - then compare that to your code.

Then you'll spot that when you set the cookie, the second if condition returns false - not true. Why? Because you seem to assume $_REQUEST["loggedin"] changes value by itself. Unfortunately PHP is not psychic - you need to unset this yourself.

Flowcharts are a great help - requires only paper, a pen and some ability to draw lines...:)
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

Post by guest »

Tried That Before.

Tried A Whole Load Of Things Tonight.

Eventually, Decided To Do It In Trusty Old JavaScript.
So, Now I Have Got A Working System.

But I Still Wonder Why......
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

is ur page in frames? cause u cant use cookies in frames
guest
Forum Commoner
Posts: 25
Joined: Sat Nov 22, 2003 11:50 pm

Post by guest »

Yes, It Was..........

Anyway, As I Said, I Used JavaScript, And It Works
Post Reply