Hey guys, I'm modifying a classified site, it's written in PHP.
What I'm trying to do is when a user comes in the website for the first time (aka no cookies created) the value of the variable would be -1, when they click on a state to browse in, it will have ?statechosen=1&blahblah, so the value would be 1 for a variable, and if its 1 it won't ask for the states again, it will only ask for the states when its -1, or 0.

but it's weird.
i have to click on the name of the state twice to have the cookie settings in effect. And if i go on the index page twice, it'll ask me to choose the state again.

here's the code.

<?php $country_url = "?statechosen=1&cityid=-$country[countryid]&lang=$xlang;" ?>
function xSetCookie($name, $value)
{
	setcookie($name, $value, 0, "/");
}
setcookie($ck_choosestate, $xstatechosen, time()+(60*24*60*60), "/");
$xstatechosen ="";
	$statechosen ="";
$ck_choosestate		= "xzclf_choosestate";
//Choose State
$nostate = -1;
if ($_GET['statechosen'] > 0)
{
	$xstatechosen = $_GET['statechosen'];
}
elseif ($_GET['statechosen'] < 0)
{
	$xstatechosen = $_GET['statechosen'];
}
elseif ($_COOKIE[$ck_choosestate] > 0)
{
	$xstatechosen = $_COOKIE[$ck_choosestate];
}
elseif ($_COOKIE[$ck_choosestate] < 0)
{
	$xstatechosen = $_COOKIE[$ck_choosestate];
}
elseif ($nostate)
{
	$xstatechosen = $nostate;
}

Recommended Answers

All 2 Replies

Not clear enough with your code, i think there should be something with flow -
You want to set the var in cookies to 1 once visited otherwise it will be -1,\
so once your module starts, set this var in cookie with the -1 value to it and once the user first visits to it check with the value of it on that page and if its -1, set it to the 1 or else nothing.This way it should work

Hey.

My spelling is particularly bad today, so let me just offer this example :)

<?php
// Check if the stat has already been selected
// and saved into the cookie
if(!isset($_COOKIE['chosen_state_id']) || intval($_COOKIE['chosen_state_id']) == 0)
{
    // See if a user has selected a state from the
    // form, printed below.
    if(isset($_GET['chosen_state_id']))
    {
        // Get the selected state from the GET value
        // and add it to the cookie.
        $new_state = intval($_GET['chosen_state_id']);
        if($new_state > 0)
        {
            setcookie('chosen_state_id', $new_state, time()+(60*60*24*30), '/');
        }
        else
        {
            echo "Error. You must select a valid state from the form!".
        }
    }
    else
    {
        // Print a "select state" form.
        // Obviously not what you use, but this i just
        // and example.
        echo <<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="get">
    <input type="text" name="chosen_state_id"/>
    <inpyt type="submit">
</form>   
HTML;
    }
}
else
{
    // Fetch the selected state from the cookie.
    $chosen_state = intval($_COOKIE['chosen_state_id']);
    
    // ... And do whatever you want to do with the
    //     now permanently selected state.
}
?>

Basically, if the cookie is set, you use the value in it. If it is not, you have the user select one and create the cookie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.