Jump to content

Headache with sessions


etonB

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.