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
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
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.
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.
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
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.
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 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: