Reputation: 57
$user_country
in this way:country=
in the URL I want $user_country
to be the country from the database; when I have country=United States
in the URL I want $user_country
to change from it's original country from the db to United States and stick to it's value until the next URL value change of country
;
session_start();
$emailc=$_SESSION["email"];
$req1=mysql_query("SELECT country FROM users WHERE email='$emailc'");
$countryz= mysql_fetch_assoc($req1);
//Get country from URL:
$country = isset($_GET['country']) ? $_GET['country'] : null;
//Check the URL to see if we change $user_country
if(isset($country)){
$_SESSION['country']=$_GET['country'];
}
else{
$_SESSION['country']=$countryz['country'];
}
//Assign value to `$user_country`
$user_country=$_SESSION['country'];
www.example.com?country=United States
In this case $user_country
=United States.
After I refresh $user_country
comes back to it's value from the database.
I want $user_country
to be equal to United States even after refresh, until the value of the country in URL changes again;
Until www.example.com?country=Canada, I want $user_country
=United States, so I can use the value $user_country
of it in my future conditions
Upvotes: 1
Views: 152
Reputation: 447
There are a few important bugs.
You should:
1) Database should select when the else statemenet is using.
2) I think that you want to use if statement in
$country = isset($_GET['country']) ? $_GET['country'] : null;
but I think you should use
$country = (isset($_GET['country'])) ? $_GET['country'] : null;
Instead null, set it to value. You don't need to make second if.
Upvotes: 0
Reputation: 2058
I think your problem occurs in this line
$country = isset($_GET['country']) ? $_GET['country'] : null;
Your code override country to null if you refresh page Thats why you have to check if $_GET not exist then get country from session
if ( isset($_GET['country']) ) {
$country = $_GET['country'];
} else {
$country = isset($_SESSION['country']) ? $_SESSION['country'] : null;
}
Your full code:
session_start();
$emailc=$_SESSION["email"];
$req1=mysql_query("SELECT country FROM users WHERE email='$emailc'");
$countryz= mysql_fetch_assoc($req1);
//Get country from URL:
// Add this code
if ( isset($_GET['country']) ) {
$country = $_GET['country'];
} else {
$country = isset($_SESSION['country']) ? $_SESSION['country'] : null;
}
//$country = isset($_GET['country']) ? $_GET['country'] : null;
//Check the URL to see if we change $user_country
if(isset($country)){
$_SESSION['country']=$_GET['country'];
}
else{
$_SESSION['country']=$countryz['country'];
}
//Assign value to `$user_country`
$user_country=$_SESSION['country'];
Upvotes: 1
Reputation: 6539
Write your code as below:-
session_start();
$emailc=$_SESSION["email"];
$req1=mysql_query("SELECT country FROM users WHERE email='$emailc'");
$countryz= mysql_fetch_assoc($req1);
//Get country from URL:
$country = (isset($_GET['country']) && !empty($_GET['country'])) ? $_GET['country'] : $countryz['country'];
//Set session variable
$_SESSION['country']=$country;
//Assign value to `$user_country`
$user_country=$_SESSION['country'];
Hope it will help you :)
Upvotes: 0
Reputation: 193
When your user log in or start to bowse your site you have to give a basic value to the country in the $_SESSION
.
if (empty($_SESSION['country'])) {
$_SESSION['country'] = initializeCountry();
}
After the initial set you have to change the value only if the $_GET parameter is set, so your code looks like this:
if(isset($country)){
$_SESSION['country']=$_GET['country'];
}
Without the else statement.
Upvotes: 2
Reputation: 2183
I think when the user navigates away you are setting the value from DB into the session. So updating your code(adding a elseif) should work
if(isset($country)){
$_SESSION['country']=$_GET['country'];
} elseif (empty($_SESSION['country'])) {
$_SESSION['country']=$countryz['country'];
}
Upvotes: 2