Session problems in php

I am using sessions to store some user information so that it is accessible for other php files that do a specific task.

i set the $_SESSION[“user_id”] = 2351351 when the user logs into the page, which is index.php

session_start();

$_SESSION["user_id"] = 2351351 ;
	
$loggedin = false;

if($_SESSION["user_id"] != 0){
	$loggedin = true;
}
 if($loggedin == true){
file_get_contents("header.php");
}

in the header.php i want to access the user id so that i can retrieve data such as the name from the user.
but it wouldn’t work
code i use in header.php

<?php 

session_start();
 $text = "My";
 if(isset($_SESSION["user_id"]) && $_SESSION["user_id"] != 0){
	$text = explode(User::getUser($mysqli, USER_TABLE, $user_id)->name, " ")[0] . "'s";
}

print("<script>alert('$_SESSION[user_id]');</script>");
?>

<center>
<h1><a href="http://EXAMPLE.tk/"><span style="color: red;"><?php print $text; ?></span> <span style="color: green;">Private</span> <span style="color: blue">Blog</span></a></h1>
<hr style="margin: 0;">
</center>

the last line print("<script>alert('$_SESSION[user_id]');</script>");displays nothing

how do i fix this error?
thank you in advance

i see that i am also having a folder namet tmp and there is one file that seems to have stored the php session i mentioned above
but i don’t understand why I am not able to access the $_SESSION[“user_id”] from my header.php file which btw is inside a directory

You said you set the session id to 2351351 but in the code you added it says 454231. Which one do you mean to have?

No, it was actually some other problem.
file_get_contents() opens a new thread to access the file and doesn’t use the one the user uses. Therefore $_SESSION cookies aren’t accessible to the file I want to get the contents from.

The id was just an example. Sorry about my mistake.

Hi @ichhabsdrauf!

I have never worked with sessions in my life. I always choosed to work with my own cookies, however I will try to debug the code, and note some things up.


1̶.̶ ̶T̶h̶e̶ ̶s̶y̶n̶t̶a̶x̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶c̶o̶d̶e̶ ̶i̶s̶ ̶i̶n̶c̶o̶r̶r̶e̶c̶t̶:̶

p̶r̶i̶n̶t̶(̶"̶<̶s̶c̶r̶i̶p̶t̶>̶a̶l̶e̶r̶t̶(̶'̶$̶_̶S̶E̶S̶S̶I̶O̶N̶[̶u̶s̶e̶r̶_̶i̶d̶]̶'̶)̶;̶<̶/̶s̶c̶r̶i̶p̶t̶>̶"̶)̶;̶

Y̶o̶u̶ ̶c̶a̶n̶’̶t̶ ̶a̶c̶c̶e̶s̶s̶ ̶a̶n̶ ̶a̶r̶r̶a̶y̶ ̶v̶a̶l̶u̶e̶ ̶w̶i̶t̶h̶i̶n̶ ̶a̶p̶o̶s̶t̶r̶o̶p̶h̶e̶s̶.̶ ̶I̶t̶ ̶s̶h̶o̶u̶l̶d̶ ̶b̶e̶ ̶l̶i̶k̶e̶ ̶t̶h̶i̶s̶:̶

p̶r̶i̶n̶t̶(̶"̶<̶s̶c̶r̶i̶p̶t̶>̶a̶l̶e̶r̶t̶(̶'̶"̶ ̶.̶ ̶$̶_̶S̶E̶S̶S̶I̶O̶N̶[̶"̶u̶s̶e̶r̶_̶i̶d̶"̶]̶ ̶.̶ ̶"̶'̶)̶;̶<̶/̶s̶c̶r̶i̶p̶t̶>̶"̶)̶;̶


2̶.̶ ̶W̶h̶e̶n̶ ̶u̶s̶i̶n̶g̶ ̶s̶e̶s̶s̶i̶o̶n̶_̶s̶t̶a̶r̶t̶(̶)̶,̶ ̶t̶h̶e̶ ̶P̶H̶P̶ ̶e̶n̶g̶i̶n̶e̶ ̶a̶s̶s̶i̶g̶n̶s̶ ̶a̶n̶ ̶u̶n̶i̶q̶u̶e̶ ̶c̶o̶o̶k̶i̶e̶ ̶t̶o̶ ̶t̶h̶e̶ ̶c̶l̶i̶e̶n̶t̶ ̶w̶h̶i̶c̶h̶ ̶e̶x̶p̶i̶r̶e̶s̶ ̶a̶t̶ ̶t̶h̶e̶ ̶e̶n̶d̶ ̶o̶f̶ ̶t̶h̶e̶ ̶s̶e̶s̶s̶i̶o̶n̶ ̶(̶w̶h̶e̶n̶ ̶u̶s̶e̶r̶ ̶c̶l̶o̶s̶e̶s̶ ̶t̶h̶e̶ ̶b̶r̶o̶w̶s̶e̶r̶)̶.̶ ̶T̶h̶e̶r̶e̶f̶o̶r̶e̶,̶ ̶u̶s̶i̶n̶g̶ ̶s̶e̶s̶s̶i̶o̶n̶_̶s̶t̶a̶r̶t̶(̶)̶ ̶o̶n̶ ̶h̶e̶a̶d̶e̶r̶.̶p̶h̶p̶ ̶a̶s̶ ̶w̶e̶l̶l̶ ̶m̶i̶g̶h̶t̶ ̶m̶i̶x̶ ̶u̶p̶ ̶t̶h̶e̶ ̶c̶o̶o̶k̶i̶e̶s̶ ̶a̶n̶d̶ ̶o̶v̶e̶r̶w̶r̶i̶t̶e̶ ̶t̶h̶e̶ ̶p̶r̶e̶v̶i̶o̶u̶s̶ ̶o̶n̶e̶s̶.̶

I̶f̶ ̶y̶o̶u̶ ̶w̶a̶n̶t̶ ̶t̶o̶ ̶w̶o̶r̶k̶ ̶w̶i̶t̶h̶ ̶s̶e̶s̶s̶i̶o̶n̶s̶,̶ ̶i̶n̶p̶u̶t̶ ̶s̶e̶s̶s̶i̶o̶n̶_̶s̶t̶a̶r̶t̶(̶)̶ ̶o̶n̶l̶y̶ ̶o̶n̶c̶e̶.̶ ̶I̶n̶ ̶a̶d̶d̶i̶t̶i̶o̶n̶,̶ ̶i̶f̶ ̶y̶o̶u̶ ̶w̶a̶n̶t̶ ̶t̶o̶ ̶d̶e̶s̶t̶r̶o̶y̶ ̶a̶ ̶s̶e̶s̶s̶i̶o̶n̶,̶ ̶u̶s̶e̶ ̶s̶e̶s̶s̶i̶o̶n̶_̶d̶e̶s̶t̶r̶o̶y̶(̶)̶.̶

You are misunderstanding my problem

Reply to 1. => my syntax isn’t wrong. PHP allows you to access values within apostrophes, which is really helpful.
Reply to 2. => that wasn’t my problem. I wanted to access a file while the user was logged in but couldn’t bc file_get_contents() wasn’t using the same thread as the user and therefore the cookies of the user which was logged in couldn’t be accessed from the other file and i would get an error.

Reply to 1. => my syntax isn’t wrong. PHP allows you to access values within apostrophes, which is really helpful.

True… Both answers were wrong actually. Sorry :sweat:

Reply to 2. => that wasn’t my problem. I wanted to access a file while the user was logged in but couldn’t bc file_get_contents() wasn’t using the same thread as the user and therefore the cookies of the user which was logged in couldn’t be accessed from the other file and i would get an error.

You want your file content to be displayed on the webpage? You want to redirect to that file? I don’t know exactly what you mean by I wanted to access a file.

No Problem :slight_smile:

exactly. but now i used a get request file_get_contents("website.com/directory/file.php?id=$_GET[id]") to get the data i needed to display on the webpage. and it is secure bc since this is written in php there is no way a user would figure out the file path and therefore everything would be secure.

I couldn’t figure out a better way. Perhaps there are some like the ones wordpress uses to get the header.php and footer.php but i don’t have enough patience to look an sb else’s code

right now i accidentally deleted .htaccess file. am looking for a replacement
just need the basic lines that 000webhost automatically generates.
pls send them to me pls

exactly. but now i used a get request file_get_contents(“website.com/directory/file.php?id=$_GET[id]”) to get the data i needed to display on the webpage. and it is secure bc since this is written in php there is no way a user would figure out the file path and therefore everything would be secure.

I was about to suggest the same thing :wink: But actually this wouldn’t work with $_SESSIONS because file_get_contents() performs server-side request, and no sessions would have been assigned to the server.

right now i accidentally deleted .htaccess file. am looking for a replacement
just need the basic lines that 000webhost automatically generates.
pls send them to me pls

The .htaccess is auto-generated, however this is the default line:

php_value display_errors 1
1 Like

thank you @NGiNX.
if you have any better ideas regarding my file_get_contents and sessions problem pls let me know

I am noob when it comes about sessions, I admit.

Every approach is good, depending on the situation, that’s what I think.

If you have any other questions, please do create new posts :wink:

1 Like

i have same problem.
i use session on my login page. but, data on session will lost if i move to another page.

I’ll have a look at your code
Please paste your login page php code and the page you want the session to be in php code.

I have same problem with similar code of ichhabsdrauf. But works perfectly when i tried with a localhost using wamp server


Solved :smile:. On root folder i removed the tmp folder, just create again and sessions works :sweat_smile: