etonB Posted March 15, 2010 Share Posted March 15, 2010 I've been trying to succesfully finish the Login part of my website to no avail. Everything works fine, and the user does get logged in right away, but the session is not maintained and I've ran out of ideas/help by googleing so I hope I can get a couple pointers here. Here's a bit of the code I'm using: <?php if ( !isset($_SESSION["username"]) ){ if ( !isset($_POST["frm_enviarLogin"]) ){ ?> <div class="avisoForm"></div> <form class="formLogin" id="form_Login" method="post" action="<?php echo $PHP_SELF;?>"> <input type="text" id="frm_username" name="frm_username" size="12" title="No mas de 12 caracteres"></input><label for="frm_username"> Username</label> <input type="password" id="frm_password" name="frm_password" size="12" title="No mas de 12 caracteres"></input><label for="frm_password"> Contraseña</label> <button type="submit" id="frm_enviarLogin" name="frm_enviarLogin">Entrar</button> <?php } else { if ( ($_POST["frm_username"] == "admin") && ($_POST["frm_password"] == "pass") ) { $_SESSION["username"] = "admin";?> <p class="textoIngresar">Bienvenido, <?php echo $_SESSION["username"];?></p> <?php } } } ?> Now, this code is placed in 'sidebar.php' and the thing is I have a 'header.php' on my website which is called with include (along with sidebar.php) from all of the diferent pages within the website. Where should I place session_start() for this to work? I've tried only adding it in 'header.php' but afaik it didn't work (the user gets logged in, but when moving to a different page the user is logged out now), and I've also tried placing session_start() everywhere, again with no results. I'm pretty sure I'm doing some newbie mistake here but I just can't see it. Thanks in advance, etonB. Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2010 Share Posted March 15, 2010 Where should I place session_start() for this to work? Before any characters of any kind are sent to the browser. If you are developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON, you will get a header error if something is sent to the browser before the session_start() statement. Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026506 Share on other sites More sharing options...
etonB Posted March 15, 2010 Author Share Posted March 15, 2010 This is the first line of my header.php file: <?php session_start(); include('conexion.php'); ?> Is it bad to place session_start() on every page? Or will it simply be ignored? The sidebar is about the only part of the website that doesnt include(header.php), so when it calls itself on form submit I figured it wasn't starting the session correctly, so I thought I had to add session_start() on the sidebar itself as well. Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026509 Share on other sites More sharing options...
StathisG Posted March 15, 2010 Share Posted March 15, 2010 Is it bad to place session_start() on every page? Or will it simply be ignored? wasn't starting the session correctly, so I thought I had to add session_start() on the sidebar itself as well. It's not optional, it's necessary to place it in the start of every page (before any characters of any kind are sent to the browser like PFMaBiSmAd mentioned). Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026512 Share on other sites More sharing options...
etonB Posted March 15, 2010 Author Share Posted March 15, 2010 I don't mean to come off as stubborn here but I still don't quite get it. Is it not enough with adding an include(header.php) in every page if the header file has session_start() as it's first line? Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026520 Share on other sites More sharing options...
StathisG Posted March 15, 2010 Share Posted March 15, 2010 If the header.php file is part of every page in your system then there is the place you should have the session_start() and the sessions should work. Are you sure you have a problem with the sessions and not a problem with your program's logic? Try to set a sample session variable to one page and echo it to another one to see if the sessions are working fine. Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026528 Share on other sites More sharing options...
etonB Posted March 15, 2010 Author Share Posted March 15, 2010 I just re-tried and changed a bit of structure and the sessions now work when tested @ http://localhost However, when uploaded to the actual website (hosting with www.ipage.com, no php.ini access) the sessions are still not maintained. Is there a setting I must/can override in htacess in order to make sessions work? Should I start a different topic and mark this as solved since the problem is a bit different now? Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026538 Share on other sites More sharing options...
etonB Posted March 15, 2010 Author Share Posted March 15, 2010 It turns out this was a problem on the server's side. I talked with their tech support and they "set the session path for my account". My sessions work perfectly fine now. I will now mark this solved, thanks to StathisG and PFMaBiSmAd for their input on session_start() placement. Quote Link to comment https://forums.phpfreaks.com/topic/195339-headache-with-sessions/#findComment-1026585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.