Why I can't start session and echo my session id? - php

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?

Related

PHP session_start returns false, $_SESSION variables are not persistent

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.

Something wrong with cPanel website.

I uploaded my website yesterday and everything was fine. I made some changes today and a lot seems to have been broken. For example
if(!isset($_SESSION['u_email'])){
header("Location: home.php");}
no longer works. I can view the webpage as someone with no account. Although other code that requires log in works fine. Even just
header("Location: home.php");
at the start doesn't redirect. And I have the correct .htaccess. I also made a complete copy of my files and it ran fine on a local server.
Also I can't get this to work on either servers.
<? if(isset($_SESSION['u_email'])){
echo '<p>
<form class="fav" action="includes/favourite.inc.php?img_id=<?=$images[0]->img_id?>" method="POST">
<button type="submit" name="submit">Add to gallery</button>
</form>
<p>';}?>
I've spent a long time trying to figure it out so I appreciate your help.
I think you didn't Started Session
add the session start top of your pages
session_start();
and if it not works check your server php.ini whether session variables are set..
look for php short tags, http session secure.. etc.

PHP sessionID changing between pages

Can anyone help me with this issue?
I want to use sessionvariables in PHP. I use session_start() on top of every page (no browseroutput before this).
I noticed that I lost the session between my pages. So I checked the sessionId. And it changed, so thats the problem.
But I also have a sessionbased login in php on the same website/domain and this works fine! And when I'm logged in, the sessionvariables on the other two pages also work fine! The sessionId stays the same then.
I looked for a difference. I found one: the session for the login is initiated from a page located at mydomain.com/php/ and my other pages are in the root.
So I tested my use of the sessionvariables with the two pages inside mydomain.com/php and now it works fine for those two!
But how is that possible? and how can I fix this so my pages in the root can also use sessionvariables without losing the session between the pages in the root?
Used code on both pages:
<?php session_start();
echo session_id();
?>
path = '/'
domain = ' ' and I tried '.mydomain.com' but didn't work.
save_path = 'C:\Windows\temp'.
I checked, but 'C:\Windows\temp' is not writable, could that be the problem? But how is it possible that my login function still works? And my two other testpages?
Also on page reload, the sessionid changes.
I changed the PHP version from 7.09 to 5.60 and now it works fine! What has changed in php sessions since PHP 5.60? I can't figure it out. Anyone an idea?

Missing session variables and cookies

My PHP code works fine locally but when I upload it to my host provider it is incapable of passing session variables and cookies. Both PHP configuratoins seem to be identical. I wrote two simple scripts (no bells or whistles included) as examples for anyone to analize and provide a solution, if possible. test2.php displays no values whatsoever. Will appreciate your help very much.
<?php
session_start();
$_SESSION["testVar1"] = "Marlon";
$_SESSION["testVar2"] = "Brando";
setcookie("actor2","glenn ford",time()+60,"/");
header("Location: ./test2.php");
exit();
?>
<?php
session_start();
echo "Actor 1: ".$_SESSION["testVar1"]." ".$_SESSION["testVar2"]."<br>";
echo "Actor2: ",$_COOKIE['actor2'];
?>
Have you checked with your provider as to whether they are allowing session state variables?
Is your hosting provider using apache? if so, I think they need to add the mod_session module. This is not installed by default as far as I remember.

Not Able to start a session on a hosting server

I checked all the other possible duplicates before posting but none of them solved my problem
Everything was working perfectly fine when using localhost, now i'm trying to upload it on a host server, but there seem to be a problem when i try to start a session..
This is the part of code concerned:
header('refresh:5;URL= ./info.php');
print "<br/>Signed in Successfully<br /> You will be redirected shortly";
session_start();
$_SESSION["login_user"]=$_POST["inputUsername"];
var_dump($_SESSION);
And this is the corresponding output:
As you can see from the code, After the session was started, The word "Started" should be printed, which is not the case, However the print statement just before the session_start was executed.
Any idea what could be the problem?
Thank you
You should always start your session first thing. Starting your session needs to send a header, which does not work if you've allready send one (for instance, explicitly, but also implicitly by e.g. doing output.
Always do your session_start() first.

Categories