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.