I am looking to set a session variable based on a search conducted by the user. The idea is that the search is populated with their last search wherever they go on the site.
I have the following code that I thought would set the variable if the variable geo-box was present and use the saved variable if it isn't, but this doesn't work...
session_start();
if(isset($_GET['geo-box'])){
echo $_SESSION['town'] = $_GET['geo-box'];
} else {
echo $_SESSION['town'];
}
session_start();
if(isset($_GET['geo-box']))
$_SESSION['town'] = $_GET['geo-box'];
echo $_SESSION['town'];
You can't echo a variable while defining it.
Best of Luck!
You are trying to echo a variable and set it in the same line.
Try this:
session_start();
if( isset($_GET['geo-box']) ) {
$_SESSION['town'] = $_GET['geo-box'];
}
echo $_SESSION['town'];
You can not echo a value and assign it at the same time. Give this a try!
Hope this helps.
Related
How to access the incremented value of session on the same page where we have defined the initial value of session, I am using this method for incrementation of value but on another page
<?php
$_SESSION['indexValue'] = 1;
?>
This is my test.php page where i have decrelaed the inital value of session.
<?php
$Incrementvalue = $_SESSION['indexValue'];
$counter = (int)$Incrementvalue;
if ($counter=$counter) {
$counter++;
$_SESSION['indexValue'] = $counter;
}
echo "$_SESSION['indexValue']";
?>
This is my getdata.php page where I have implmented the increment value function. Now i have to pass this increment value of Session again on test.php page . How can I perform this?
your code seems not to be thought off
first of all, you need to call session_start() at beginning
then you can access everyewhere your $_SESSION variable (if you call session_start() before)
if ($counter=$counter) {
$counter++;
$_SESSION['indexValue'] = $counter;
}
this seems useless
$_SESSION['indexValue']++;
is enough
First of all there are few errors in code like
if ($counter=$counter) should be if ($counter == $counter)
And
echo "$_SESSION['indexValue']" should be echo $_SESSION['indexValue'].
Now answer to your question is that session is accessible anywhere throughout the project/application. So you need not to pass it , just access it as $_SESSION['indexValue'].
This is what I have.
<?php
if (isset($_GET["var"])) {
echo "1";
} else {
echo "1";
}?>
I want it to check if there's been a variable set for "var" and if there isn't (which will happen the first time anyone goes to site), I want it to be set to "1". Then I want to create buttons "back" and "forward" that will will increase "var" by "1" until it reaches my limit of "10" then returns to one.
I can figure out the math and buttons part, I just need help initially setting "var" since it isn't already set when you go to the page.
Aside from architecture, security and any other issues:
$_GET is just a simple array accessible from anywhere in script.
$_GET['var'] = 'foo';
Docs: http://www.php.net/manual/en/language.variables.superglobals.php
Try this tag
<a her="www.example.com?var=<?php echo $get_var?>">your site name</a>
the get variables come in to the script like so
scriptname.php?getvar=val
To "set" the variable you would need to test for it, Increment it and redirect too it
$var = 1;
if (isset($_GET['var'])){
$var = (int) ($_GET['var'] +1);
if($var > 10) {
$var =1;
}
}
header("Location: " . $_SERVER['PHP_SELF'] ."?var=" . $var);
as mentioned in the comments, SESSION is a much better place to store this information
How would I for example, take a url with some $_GET data, for example http://www.website.com/something?food=steak
How would I then output steak? My current situation is that I'm trying to use the Header function to redirect to a page where I have it so that if $_GET["duplicate"] is equal to 1, then echo this, else, echo nothing. But its not taking the $_GET data I can tell I did a var_dump($_GET);
<?PHP if ($_GET["duplicate"] == 1 )
{
echo "<h1>Username Taken!</h1>";
}
else
{
echo "";
}
?>
The above is using the url http://something.com/register?duplicate=1
It's just a variable, treat it like one:
echo $_GET['food'];
Everything after question mark is available in form of global array $_GET.
$a=$_GET["food"];
echo $a;
also
if url has ?food=steak&color=red;
$a=$_GET["food"];
$b=$_GET["color"];
more than one is possible. Also search for $_POST.
Alright, so I figured my issue out. I have a $_GET variable that gets the end of the page and declares it as "p" for page. I need to do the following to get it to work.
?p=createuser&duplicate=1
I think this code should echo "first" in first usage and after refresh it should echo timestamp,but it will show first every time,where is my problem?
I set cookie permition on always.
if (isset($_SESSION['TestSession']))
{
echo ($_SESSION['TestSession']);
}
else
{
echo "first";
$_SESSION['TestSession'] = time();
}
Have you placed
session_start();
at the top of your page?
To start a session, first session_start() should be called. Otherwise $_SESSION won't be set, and you will initialize it every time.
I am using Magento and trying to save a value in the session as follows in its index.php file, but the value is not being retained.
$_SESSION['myvar'] = '1';
How do I do it?
Thanks
Let's say you want to save the value "Hello world" to the "welcome message" variable in the session. The code would be :
$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);
Now you want to echo the "welcome message" somewhere else in your code/site.
$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);
Following the example given by Ali Nasrullah, I would do:
$session = Mage::getSingleton("core/session", array("name"=>"frontend"));
// set data
$session->setData("device_id", 4);
// get data
$myDeviceId = $session->getData("device_id");
Make sure you include [Mage-root]/app/Mage.php befor calling the code above!
#Ali Nasrullah: Pass the value of device:id as second parameter of the setData function.
Mage::getSingleton('core/session')->setMySessionVariable('MyValue');
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;
Take Look For More:
Here are code to Get, Set, and Unset Session in Magento
Here are code to Get, Set, and Unset Session in Magento
frontend: Mage::getSingleton('core/session')->setYourNameSession($session_value);
backend: Mage::getSingleton('admin/session')->setYourNameSession($session_value);