Jump to content

Recommended Posts

ok so i dont know how to cook but this is what i need help with

 

i have a classified site that I am trying to use cookies to set it with states (ie. query=( WHERE state = $_cookie['state'])

 

index.php is the main coding system of the site so everything goes through index.php (ie. index.php?a=32b=43)

 

i want to use if(empty($_cookie['state'])) to see if the cookie is set or not for state and then it will show up their listings

 

 

here is what it looks like

//THIS IS ON index.php PAGE
$cookie_state = $_COOKIE['state_nvc'];
if(!isset($cookie_state))
{
include "states.php";
}
elseif (isset($cookie_state))
{
run code for index.php

}

 

my problem is when it goes to the URL index.php?a=32b=43 it redirects back to states.php

 

 

can't get around it help PLEASE!!!

 

THANK YOU

Link to comment
https://forums.phpfreaks.com/topic/210477-over-cooked-_cookies-need-help/
Share on other sites

//THIS IS ON index.php PAGE
$cookie_state = $_COOKIE['state_nvc'];
if(!isset($cookie_state))
{

In this snippet, $cookie_state is set (you just set it).  You need to test the actual cookie (and you don't need the elseif that tests the exact opposite; just use else).

 

Also, the first line is/should throw an error (ok, a NOTICE, but those come from logic errors) if the cookie is not set.  Take that line out and only assign the value once you know the cookie is set.

//THIS IS ON index.php PAGE
if(!isset($_COOKIE['state_nvc'])) {
include "states.php";
} else {
$cookie_state = $_COOKIE['state_nvc'];
run code for index.php
}

 

hey david,

i used your code above for a little bit but it ended up doing the same thing when i went back to index.php page from a index.php?a=32b=43 page

 

I dont know why it keeps thing cookie is not set after it has been set.

 

is there anything else i can try?

 

thank you guys

It should remain set and available (depending on how you set it) unless

1) You change domains (including going from www.domain.com to domain.com)

2) You use the setcookie() function to erase it

3) You change directories

4) The cookie expires - what expire date did you put on the cookie?

 

Sometimes, when working with cookies, it is helpful to use your browser to delete the cookies for the site, close the browser and start again.  Can you post more (or all) of the index.php (especially any part that uses setcookie()). Also states.php (again, especially where it uses setcookie()).  Is states.php a compete page in its own right? Should you be using a header() redirect instead of the include? This could be important if there is anything happening before the include.

 

Do you have error_reporting() and display_errors turned on? Maybe you're getting an error setting the cookie.

  • 2 weeks later...

sorry haven't replied in a while, been busy.

 

so far it detects that cookie is set and uses it.....but my problem is if a user wants to change their state and create a new cookie...i'm not able to delete the old cookie.

 

this is what begins the index.php page

if(!isset($_COOKIE['state_nvc'])) 
{
include "states.php";
} else { (isset($_COOKIE['state_nvc']));

 

so it basically looks to see if the cookie is set or not and if not it sends them to index.php page

 

then say a user looks at an item and decides to change their state they would click "Change Location" and it would take them back to states.php page again

 

this time the click a state and its processes through this page

 

Process-states.php


if(isset($_COOKIE['state_nvc'])) //i check to see if they already have a cookie set

{
setcookie ("state_nvc", "", time() - 3600);          //then i set it to a negative time WHICH is supposed to delete it
setcookie ("zipcode_nvc", "", time() - 3600);
}
$get_state = $_GET['state'];
$get_zipcode = $_GET['zipcode'];
if(!empty($_GET['zipcode']))
{
$expires = time() + 31536000;

setcookie(state_nvc, $get_state, $expires);                           //then a new one is set again
setcookie(zipcode_nvc, $get_zipcode, $expires);

echo "<html><head>
<META HTTP-EQUIV=Refresh CONTENT='0; URL=http://www.mysite.com/index.php'>
</head><body>
</center><b>One Moment Please</b></center>
</body></html>";

}
else
{
echo 'Please Enter Your Zipcode<A HREF="javascript:javascript:history.go(-1)"><br>Click To Go Back</A>';
}

?>

 

my issue is that the old state cookies are not being delete when a new one is create

i got it all fixed up now!

 

for anyone that may need this in the future....here is how i got it to work

 


if(isset($_COOKIE['state_nvc']))

{
$expireNow = time() - 3600;
setcookie ("zipcode_nvc", "", time() - 3600);
setcookie("state_nvc", "", $expireNow); 
}
$get_state = $_GET['state'];
$get_zipcode = $_GET['zipcode'];
if(!empty($_GET['zipcode']))
{
$expires = time() + 31536000;

setcookie("state_nvc", $get_state, $expires, "/", ".mysite.com");
setcookie("zipcode_nvc", $get_zipcode, $expires, "/", ".mysite.com");

echo "<html><head>
<META HTTP-EQUIV=Refresh CONTENT='0; URL=http://www.mysite.com/index.php'>
</head><body>
</center><b>One Moment Please</b></center>
</body></html>";

}
else
{
echo 'Please Enter Your Zipcode<A HREF="javascript:javascript:history.go(-1)"><br>Click To Go Back</A>';
}


 

 

I specified the location and domain in the cookie. basically, it was expiring because it was setting two domains....www.mysite.com and .mysite.com so it had to set it one of them instead of both of them

 

 

hope this helps!!

 

thanks guys

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.