I'm experiencing a strange PHP session issue. Can someone tell me if that's how session works?
To see the problem, load the following code into any php file, say test.php, and run it 2 times. NOTE, you have to run it two times, i.e. load the page and reload it.
<?
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
On my server, the first time I load this test page, I get
Original////Original////
and that is correct. But when I reload it, I get
Original////New////
which means the 5th line "$test="New////";" actually rewrite my $_SESSION["test"]. That doesn't make sense to me. Anyone knows what is happening? Or it's just happening on my server???
Seems like register_globals is enabled on your server. You'll need to disable the directive.
Firstly, do not use <? as a starting tag of PHP, please use <?php. Secondly, this is an expected behavior if you have register_globals enabled. Look at this link:
http://www.theblog.ca/session-register-globals
It's title says:
When register_globals is on, session variables overwrite global variables
And sample code is similar to yours:
<?php
session_start();
$canadaday = 'July 1st';
$_SESSION['canadaday'] = 'July 2nd';
print '<p>When is Canada Day?</p>';
print '<p><strong>' . $canadaday . '</strong></p>';
?>
With register_globals, result is July 2nd. HTH.
<?php
session_start();
$_SESSION["test"] = "Original////";
$test=$_SESSION["test"];
echo $_SESSION["test"];
$test="New////";
echo $_SESSION["test"];
?>
I have tried your code in my environment its running perfectly.its always print Original////Original////
so its happening only on your server
Related
Let me start by saying that I'm relatively new to PHP, so apologies if this seems trivial. Also, apologies if a similar scenario has already been answered, but I couldn't seem to find any solutions that worked for me by browsing through the similar questions.
I am trying to set up a login page for my website, which I aim to use $_SESSION variables to store the users login credentials once they have logged in. However, I can't find a way to make the $_SESSION variables persist through to other pages, or even stay when the web page has been refreshed.
It should be noted that:
Whenever I try to call session_start(), it always returns false. I call it at the very start of my php code, without any white space before it.
Whenever I call session_status, it always returns 1. This happens regardless of wherever it is called in the php code.
Whenever I call session_id, it always returns null. Again, this happens regardless of wherever it is called in the php code.
The code is being ran on SiteGround's web hosting services. I haven't tested it locally as I am relatively new to php and I'm not quite confident on how to host php files locally.
I have tested this both on; my windows PC using chrome, and my iPhone using Safari. Both have the same issues.
Any help or advice would be greatly appreciated as something which would seem rather trivial has been driving me crazy for the past couple of days.
EDIT
The following code is what I'm using to test the issues.
<?php
if (session_start())
{
echo '<script>console.log("YES"); </script>';
} else {
echo '<script>console.log("NO"); </script>';
}
$_SESSION["test"] = "YEET";
echo '<script>console.log("'.$_SESSION["test"].'"); </script>'; // prints YEET
?>
<?php
session_start();
if (isset($_SESSION['test']))
{
echo '<script>console.log("'.$_SESSION["test"].'"); </script>';
} else {
echo '<script>console.log("NEET"); </script>';
}
?>
Update: I found the solution.
Even though SiteGround (my host) had set the session.save_path to be /tmp, I still had to write a custom php.ini file with this file path. This has since resolved the issue, and it was made clear that the session.save_path was the issue once I set the php.ini file to display both errors and startup errors.
I am using Xampp version 7.0.1-0 . I am using the default port as usual. Everything works fine but while starting a session, the browser can't access the page, rather than it says "This site can't be reached". Here is the simple php code what's I am trying:
<?php
session_start();
echo session_id();
?>
I tried different browsers but the problem still remains. Can anyone please help me?
OK.. forgive me, there are a couple similar questions posted already with the correct responses... I knew someday I would have to ask the people of the internet for help but I never thought it would be something so stupid...
here is my code.. so far..
index.php:
include "http://www.mywebsite.com/shared.php";
$page = "homepage";
include "http://www.mywebsite.com/htmlheader.php"; //trying to use "page" variable in here
echo ("<br>test2: " . $_SESSION['test']);
include "http://www.mywebsite.com/htmlfooter.php";
shared.php:
session_start();
$_SESSION['test'] = "what the f_ck im scared";
htmlheader.php:
echo ("test1: " . $page . "<br>" . $_SESSION['test']);
Output right now is:
test1:
test2:
(so the pages are being included.. just not able to use the variables..) From what I understand in its current state this should be printing something like:
test1: what the f_ck im scared
test2: homepagewhat the f_ck im scared
..The funny thing is I was not having any issue with the include using the variable. I had added some things but then it randomly stopped working so I reduced it down to this to try and figure out what the problem was.. I am assuming I have made some silly mistake.
Ensure that you have started the session with session_start();
also check whether allow_url_fopen or allow_url_include or both have been set to 0 (disabled) in php.ini. if yes try to activate it
include "htmlheader.php";
instead of
include "http://www.mywebsite.com/htmlheader.php";
ensure you have session_start(); at the top of the page that is including the includes
My code looks like this:
...
$_SESSION['message']="something";
header('location:http://url/somewhere');
exit;
As you can see, I have an exit at the end of it. And that is the problem. It doesn´t work although I have an exit there.
I have this problem only on my localhost. At the online server it works well. In the errorlog it only shows "undefined index message". A few days ago I installed new Apache 2.4 and PHP 5.4.
Don't forget to start your session on every page you are going to use it:
if(!isset($_SESSION)){
session_start();
}
Please be sure to add "session_start();" to the beginning of every page that processes any sort of session data. Another thing to check would be to make sure you are setting the session variable properly. Also, when using header to redirect, make sure there is NO white space in the beginning of your document.
Ok, maybe my brain is just shut off, but I can't get this to work.
Here's the complete code:
Page1.php:
<?php
$something = "hello";
include "Page2.php";
?>
Page2.php:
<?php
echo $something;
?>
Desired output (when navigating to Page1.php):
hello
The real output is blank. I have tried putting the global keyword everywhere, and nothing happens. Am I missing something?
I cannot replicate this error, just tried this on my localhost and copied and pasted your code from here. I suspect you have some sort of syntax error.
Turn on error reports and see if you get any errors.
I know this is a late answer, but I'm trying to do something similar. First of all, when you echo something you still have to put it in " ". Php will recognize it as a variable as long as you put the $.
Second, you're including page2.php in page1. Fantastic, but page2 does not recognize $something. Now, if you do it the other way, declare $something in page2, and then call it from page 1 after including it, it will run.
Modifying the variable would require something else...
I think the output is coming in page2.php . Am i right? this is because you are echoing a unset variable in page2.php you need to change the following data in order to make it work.
page1.php
<?php
include("page2.php");
echo $something;
?>
page2.php
<?php
$something="Hello";
?>
If you will use it and navigate the page 1.php then the output will be Hello
I had a similar problem running on local (Windows) where the values of an array did not follow past the include within the same process.
After switching the include path from http://localhost/www/example.php to C:/www/example.php, it works fine now.