Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,857 members, 7,810,285 topics. Date: Saturday, 27 April 2024 at 05:31 AM

[SOLVED!] Cookie Not Set Using Setcookie(). Help! - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / [SOLVED!] Cookie Not Set Using Setcookie(). Help! (857 Views)

Enter For All Wapka Codes/get Your Wapka Site's Problems Solved / SOLVED: Phpass Login Issue With Checkpassword / How To Store Data With javascript / Javascript Cookie Workshop (2) (3) (4)

(1) (Reply) (Go Down)

[SOLVED!] Cookie Not Set Using Setcookie(). Help! by obiscolly(m): 7:30pm On Jan 24, 2013
Hello guys.
I'm going through a php tutorial and i tried executing this script to set a cookie, but the cookie is not being set.
* authuser.php *

if ($num!=0){
$cookie_name ="auth";
$cookie_value ="ok";
$cookie_expire ="0";
$cookie_domain ="127.0.0.1";

setcookie($cookie_name,$cookie_value,$cookie_expi
re,"/", $cookie_domain,0);

$display_block ="
<p><strong>Secret Menu:</strong></p>
<ul>
<li><a href=\"secretA.php\">secret page A</a>
<li><a href=\"secretB.php\">secret page B</a>
</ul>";
---------------------------------------------
* secretA.php and secretB.php *
<?

if ($_cookie[auth] == "ok" ) {

$msg = "<P>Welcome to secret page A, authorized user!</p>";

} else {

echo "<p>not Working</p>";
}

?>
When I click the links for either page I get 'not Working' rather than the message. I've checked browser settings. This code is straight from the author- so I'm not too sure what's going on.

Thanks-
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by yawatide(f): 7:44pm On Jan 24, 2013
I don't claim to be a PHP expert but it doesn't look like your code below is correct at all. For one, you shouldn't be checking for cookie(auth) but cookie($cookie_name) - you assigned cookie_name the value of "auth".

Why not look at this simple tutorial, build from there, then do more advanced stuff:
http://www.tizag.com/phpT/phpcookies.php

good luck!
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by obiscolly(m): 8:22pm On Jan 24, 2013
yawa-ti-de:
I don't claim to be a PHP expert but it doesn't look like your code below is correct at all. For one, you shouldn't be checking for cookie(auth) but cookie($cookie_name) - you assigned cookie_name the value of "auth".

Why not look at this simple tutorial, build from there, then do more advanced stuff:
http://www.tizag.com/phpT/phpcookies.php

good luck!

Thanks for the reply. The code seemed to work for the author tho. I tried using an if- else loop just to get a message if the cookie is created or not, but i got a negative response. Well, checking out the link you sent. Hope i figure it out soon. Thanks!
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by sisqology(m): 8:38pm On Jan 24, 2013
obiscolly: Hello guys.
I'm going through a php tutorial and i tried executing this script to set a cookie, but the cookie is not being set.
* authuser.php *

if ($num!=0){
$cookie_name ="auth";
$cookie_value ="ok";
$cookie_expire ="0";
$cookie_domain ="127.0.0.1";

setcookie($cookie_name,$cookie_value,$cookie_expi
re,"/", $cookie_domain,0);

$display_block ="
<p><strong>Secret Menu:</strong></p>
<ul>
<li><a href=\"secretA.php\">secret page A</a>
<li><a href=\"secretB.php\">secret page B</a>
</ul>";
---------------------------------------------
* secretA.php and secretB.php *
<?

if ($_cookie[auth] == "ok" ) {

$msg = "<P>Welcome to secret page A, authorized user!</p>";

} else {

echo "<p>not Working</p>";
}

?>
When I click the links for either page I get 'not Working' rather than the message. I've checked browser settings. This code is straight from the author- so I'm not too sure what's going on.

Thanks-



You did not parse the bolded part.

if ($_cookie[auth] == "ok" ) {

Echo "<P>Welcome to secret page A, authorized user!</p>";
}


//you defined a variable, you didn't use it
//try and let's know now
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by sisqology(m): 8:41pm On Jan 24, 2013
obiscolly: Hello guys.
I'm going through a php tutorial and i tried executing this script to set a cookie, but the cookie is not being set.
* authuser.php *

if ($num!=0){
$cookie_name ="auth";
$cookie_value ="ok";
$cookie_expire ="0";
$cookie_domain ="127.0.0.1";

setcookie($cookie_name,$cookie_value,$cookie_expi
re,"/", $cookie_domain,0);

$display_block ="
<p><strong>Secret Menu:</strong></p>
<ul>
<li><a href=\"secretA.php\">secret page A</a>
<li><a href=\"secretB.php\">secret page B</a>
</ul>";
---------------------------------------------
* secretA.php and secretB.php *
<?

if ($_cookie[auth] == "ok" ) {

$msg = "<P>Welcome to secret page A, authorized user!</p>";

} else {

echo "<p>not Working</p>";
}

?>
When I click the links for either page I get 'not Working' rather than the message. I've checked browser settings. This code is straight from the author- so I'm not too sure what's going on.

Thanks-
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by obiscolly(m): 10:24pm On Jan 24, 2013
sisqology:



You did not parse the bolded part.

if ($_cookie[auth] == "ok" ) {

Echo "<P>Welcome to secret page A, authorized user!</p>";
}


//you defined a variable, you didn't use it
//try and let's know now
U mean the $msg variable? I used the $msg it later in the coding (in the html part). I didn't paste the complete code. Made the corrections tho, and still got d same results.
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by Gerardcole(m): 6:26am On Jan 25, 2013
Your setcookie() function is presented wrong parameters.

The "/" is not supposed.

I ve never used / heard of $cookie_domain so I guess its not needed or is wrong.

The last 0 is not needed, never heard of it.

You missed the quotes. Use either double ( " ) or single quote ( ' ) to enclose the string you are searching for.
It should be

if ($_COOKIE["auth"] == "ok"wink


Your expire time should be a time more than the current time. So add this:

$NextFourWeeks = 60 * 60 * 24 *7 * 4;

Then change $cookie_expire to:

$cookie_expire = time() + $NextFourWeeks;

Modify your set cookie code to:

setcookie($cookie_name, $cookie_value, $cookie_expire);

Leave every other parameters.
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by obiscolly(m): 2:21pm On Jan 25, 2013
Gerardcole: Your setcookie() function is presented wrong parameters.

The "/" is not supposed.

I ve never used / heard of $cookie_domain so I guess its not needed or is wrong.

The last 0 is not needed, never heard of it.

You missed the quotes. Use either double ( " ) or single quote ( ' ) to enclose the string you are searching for.
It should be

if ($_COOKIE["auth"] == "ok"wink


Your expire time should be a time more than the current time. So add this:

$NextFourWeeks = 60 * 60 * 24 *7 * 4;

Then change $cookie_expire to:

$cookie_expire = time() + $NextFourWeeks;

Modify your set cookie code to:

setcookie($cookie_name, $cookie_value, $cookie_expire);

Leave every other parameters.
Thanks man! This worked like magic! I was really surprised. Adding one more parameter made it not work. I don't understand tho, cos on the php site, the setcookie() function is said to take up to 6 parameters, and the tutorial ebook i'm using says the same too. Well i'm glad it did work finally.

Thanks again.
Re: [SOLVED!] Cookie Not Set Using Setcookie(). Help! by Gerardcole(m): 3:00am On Jan 26, 2013
I guess you should edit your first post and give this thread-name a prefix [SOLVED]

(1) (Reply)

Google Doodles 2013 / Music Applications Will Grow Into A Huge Market, / I Hate Ads Sense, Affiliate Marketing In Websites

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 25
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.