homepage Welcome to WebmasterWorld Guest from 50.16.112.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
how to fix the $ SESSION variable value?
the variable set to a variable in the superglobal $_SESSION not recognized
jerry95

5+ Year Member



 
Msg#: 4375059 posted 5:02 am on Oct 16, 2011 (gmt 0)

Hi, I write to you all cause I've been having a problem with my code to set the value of an index in the array $_SESSION.
I'm new to PHP so excuse me if I don't grasp fully the concept of sessions.
I thought it'd be enough to set the value of a variable in the sessions simply with a conditional statement like so:

I have a form where users sign in, and then I insert the $_POST variables in a database, OK, then I do:

$_SESSION['signed_in'] = true;

//we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages
while($row = mysql_fetch_assoc($result))
{
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['user_level'] = $row['user_level'];
}

echo 'Bienvenido, ' . $_SESSION['user_name'] . '. <br /><a href="forum.php">Ir a la página principal del foro</a>.';
}


The problem I had initially was that the wamp server throwed an error of Notice level saying there's an undefined index 'signed in' when I tried to view another page (in this case the main page: forum.ph) I paid no attention to the error cause it was simply a notice. But then in the server where the site is placed things didn't work. I wrote to them and they said it's finally solved, and they replied the problem was that the code to fix the start of the session wasn't present.

Now to the actual problem: Indeed the problem with the forum pages is solved now and there's no trouble logging in, but I looked at the code of all my pages and nothing changed. So I thought I should change the code in all the pages that check the value of the mentioned 'signed_in' index to a conditional statement saying:

$inicio_sesion = isset($_SESSION['signed_in']) ? $_SESSION['signed_in'] : null;
$usuario_nivel = isset($_SESSION['user_level']) ? $_SESSION['user_level'] : null;
if($inicio_sesion == true)
{
echo 'Hola <b>' . htmlentities($_SESSION['user_name']) . '</b>. No eres tú? <a class="item" href="signoutadmin.php">Cerrar sesión</a>';
}
else
{
echo '<a class="item" href="administracion.php">Iniciar sesión</a> o <a class="item" href="registryadmin.php">crear una cuenta</a>';
}


Previous to that change, the code sayed simply:

if (!$_SESSION['signed_in']) {...} else {}
I also checked the erros in other indexes of $_SESSION like $_SESSION['user-level'] and the rest of variables I'm using until all the Notices the local (testing) server I'm using stopped annoying me.

I wrote to the guys in the server and they replied that I should contact the developer of my site.
Hmm, you see the developer is me! so that's why I'm asking your help.
See the thing is that I have several things in all the pages that need logging in: I have a user bar that displays the name of the logged user and I show or not show the form to reply to a message depending on the value of the mentioned 'signed_in' index.
I also have an administrative section that also checks for that value to show or not show the client information from the database, etc.

Changing the code like I did solved the problem of the errors (the notice saying there was an undefined index on line x: ). However, I'm still unable to access the pages on the administrative section, and the guys in the server won't answer me till the end of the weekend.

So what am I doing wrong? When I test the site locally everything works fine, it even worked fine before checking the error (cause I simply set error display to none after being annoyed by the mentioned Notice). But I uploaded the administrative pages with the change I made to the code lastly and they don't work.

Can someone explain to me in plain English (or better, Spanish) how sessions work? Is there something related to how Apache works with php? I also know I can't make changes to php.ini on the server the site is in, but then how can I get the code to work? Any help will be greatly appreciated.

You can see the site at [arqintegral.com.mx...] if you may want to check how its working. I won't post more code to make this post brief, cause there's like 10 pages related to the problem.

 

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4375059 posted 11:35 am on Oct 16, 2011 (gmt 0)

an error of Notice level saying there's an undefined index 'signed in'


This doesn't just relate to sessions. You get this E_NOTICE if you try to read an array element before you have assigned anything to it. In the same way if you try to read an ordinary variable before you have declared/assigned something to it (undefined variable notice). Like you say, it is just a notice. It is trying to safe guard you from accidental mistakes (typos) in your code. PHP will implicitly return null.

$myVar = array('lang'=>'Spanish'); 
echo $myVar['language']; // Undefined index 'language' - oops, my mistake!


With a session variable (which is just an element in an array - a special superglobal array), you probably should always be checking first if an element/value exists before using it - like you have done...
$inicio_sesion = isset($_SESSION['signed_in']) ? $_SESSION['signed_in'] : null;


...and they replied the problem was that the code to fix the start of the session wasn't present.


And this was fixed by calling session_start()?

You need to call session_start() on every page - at the very start of your script - if you are using the session. As its name suggests, this starts the session, but more importantly it will resume a session that has already been started on a previous request/page... it reads the session cookie (a unique id stored on the clients computer), and loads/populates the session variables from wherever they are stored on the server. PHP handles this for you, unless you have explicitly declared your own session handler. Without calling session_start() none of your session variables from the previous page/request will be populated.

jerry95

5+ Year Member



 
Msg#: 4375059 posted 6:35 pm on Oct 16, 2011 (gmt 0)

Thanks Pender for your help. I changed the code in the administrative pages so that the session_start() function is called at the very beginning. They now work fine. I have one question: ¿does that mean that the session_start() function has to be called like the header funciton, without any other output before? I ask because I did called session start on every page before, but I called it by an include() of a file called connect.php that is used to connect to the database and also to start the session, but the include came after the doctype declaration and other stuff like the meta tags of the file.
Anyway, thanks for your reply. If I have further doubts I'll post them.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4375059 posted 8:27 pm on Oct 16, 2011 (gmt 0)

¿does that mean that the session_start() function has to be called like the header funciton, without any other output before?


Yes, since sessions generally use a cookie to store the session id, it must be called before any output. Otherwise you will get a warning, "...headers already sent...", and the cookie won't be set (or updated, if successfully set on another page).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved