Hello guyz,

I was using sessions on my php code to pass specific variable values such as usernames. Everything goes well when I go straight browsing from page 1 to page 2, 3, 4 etc. but when I press the back button or even clicking the link to another previous page which uses the variable 'username', an error occurs saying:
"Notice: Undefined variable: _SESSION in C:\xampp\htdocs\mp3site\profile.php on line 15"-This never occurs on my first pass on the page but it seems like the value I passed on the SESSION was gone.

Can anybody tell me why this is happening? Any suggestions on what I should do please??? thanks

Post your code so we can take a look.

Member Avatar for diafol

do you have session_start at the start of every page?

have you used session_regenerate_id() anywhere along the line or a session_destroy?


BUT:

Post your code so we can take a look.

No, I did'nt have session start on every page. I've only used session start on the page where the value is passed through POST form. Actually I don't know about session_regenerate_id(). Do I have to use that session_regenerate_id()?, and can you please tell me what is that for?..... thanks

You need session_start() at the top of every page where you use sessions and no you don't need session_regenerate_id() (that question was in case you were using it in error).

So I have to put session_start() on every page?... AS IN ALL PAGES? But wouldn't it reset the sessioned values of variables?....? ? Cuz I was thinking that maybe that would happen. Pls reply

Yes, on all pages, no, it won't reset. You may want to read the manual.

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Thank you so much for the idea!... I'll try to modify my code and I'll post again if I succeeded.

Sir, I tried to put session_start on all pages using sessions. I did put session_start() on the second page to which receives the variable value from Page 1 but when I ran the code an error occurs saying "Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\mp3site\profile.php on line 12".

It needs to be at the very top of the page and you need to ensure you are not calling it more than once (i.e. from an included file). So you would have something like

<?php
session_start();
//followed by your include/require files and any other PHP
?>
<!-- Your HTML page starts here -->
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.