Jump to content

Problems with session_register and $_SESSION


Kristian_C

Recommended Posts

Hey guys/gals :)..I've taken a break from php/sql/html/css for a while now but now i decided to start again for real :).. I was going to make a login function that registers the session username so the people on my site chould register and make profiles, send messages, be in the forum etc.etc but when i was done with the script it dident work, ive been trying to fix it for a while now but it seems like a endless error is bothering me here...Just to say abit of the script i can write down abit of the code that might be the problem here...

login.php <?if(isset($_POST['login'])){$username=$_POST['username'];$password=$_POST['password'];$checkuser=mysql_num_rows(mysql_query("SELECT username FROM users WHERE username='$username'"));if($checkuser =="0"){die('No such user');}$checkpass=mysql_fetch_object(mysql_query("SELECT password FROM users WHERE username='$username'"));if($password!=$checkpass){die('Password is wrong');}else{$users=mysql_fech_object(mysql_query("SELECT * FROM users WHERE username='$username'"));$user=$users->username;session_register($user);header("Location: iindex.php");}}?>allothersites.php<?session_start();$username=$_SESSION['user'];?>dessession.php<?session_start();$username=$_SESSION['user'];if(empty($_SESSION['user'])){header("Location: session-exp.php");}?>

Well the real code is in norwegian and somethings are different but about every code line is the same, everything that can be wrong that is...So if anyone can help me to sort something out or maybe write a peace of code that will work whould be verry nice :)..-Kristian_C

Link to comment
Share on other sites

The errors are :Warning: session_register(): Cannot send session cookie - headers already sent by (output started at /home/content/u/n/i/united17/html/nettby/define-test.php:4) in /home/content/u/n/i/united17/html/nettby/define-test.php on line 5Warning: session_register(): Cannot send session cache limiter - headers already sent (output started at /home/content/u/n/i/united17/html/nettby/define-test.php:4) in /home/content/u/n/i/united17/html/nettby/define-test.php on line 5Warning: Cannot modify header information - headers already sent by (output started at /home/content/u/n/i/united17/html/nettby/define-test.php:4) in /home/content/u/n/i/united17/html/nettby/define-test.php on line 6this is from a different script, but its the same errors.

Link to comment
Share on other sites

hmm, my real codes will prob'z loock like this on most .php files :

<?session_start();include"mydbconnect.php";include"whateveriwanttoinclude.php";$username=$_SESSION['user'];//More and more and more code :p..?>

Link to comment
Share on other sites

Ill make an example :Here's the connect file :

<?$mysql_user="user";$mysql_host="host";$mysql_db="db";$mysql_pass="pass";$sql=@mysql_connect($mysql_host,$mysql_user,$mysql_pass);		@mysql_select_db($mysql_db);if (!$sql) { 	die();}?>

Here's the other include file :

<?session_start();$username=$_SESSION['user'];if(empty($_SESSION['user'])){header("Location: session-exp.php");}?>

Here's the site the user is on :

<?session_start();include"mydbconnect.php";include"check-exp.php";$username=$_SESSION['user'];//more more and more code :p...?>

Well thats all i guess..

Link to comment
Share on other sites

Hmm, when i now look at the code, i see that i call the session 2 times in the "same" page, so that may solve that problem, but whats the header's problem? btw, in the login file i dont include the other file, just in the site iindex.php, so ill try to remove it from there, but it's still a problem with header and session_register...

Link to comment
Share on other sites

The header error from session_register is because you used session_register without using session_start first. It's better not to use session_register at all, it's better to just use the $_SESSION array. With $_SESSION you still need to call session_start before you use it, but you should have a call to session_start the first thing. You can't send any output before you use session_start, or else you will get that header error message. That message means that it can't send the header for the session cookie because all of the headers were already sent, and that happens whenever you have output in the script. The error message shows you where that output is that is the problem:output started at /home/content/u/n/i/united17/html/nettby/define-test.php:4Call session_start before any output.

Link to comment
Share on other sites

The header error from session_register is because you used session_register without using session_start first. It's better not to use session_register at all, it's better to just use the $_SESSION array. With $_SESSION you still need to call session_start before you use it, but you should have a call to session_start the first thing. You can't send any output before you use session_start, or else you will get that header error message. That message means that it can't send the header for the session cookie because all of the headers were already sent, and that happens whenever you have output in the script. The error message shows you where that output is that is the problem:output started at /home/content/u/n/i/united17/html/nettby/define-test.php:4Call session_start before any output.
Hmm, just wrote session_start(); right after the <?php tag, but it wont work, i still get the error : Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/u/n/i/united17/html/parkour/start-v2.php:5) in /home/content/u/n/i/united17/html/parkour/logginn.php on line 2 :s, cant understand any of this :s...
Link to comment
Share on other sites

Unless it is erroring on a subsequent session_start() somewhere later on. Remove all session_start()s except for the very first one.

Link to comment
Share on other sites

It won't try to send a second session cookie, a second call to session_start will issue a warning and do nothing else. The error message is saying exactly where the problem is, there is output on that file and that line and the call to session_start needs to come before that, that's all you have to do to fix this.

Link to comment
Share on other sites

Ok, now i made the code as an "extern" code, and it dont return any errors and it does send me to the site i want to, but the dessession.php sends me back to the login page, so i dont think it registrers the session :s.. Any ideas?Thanks for ur help so far tho :)

Link to comment
Share on other sites

Every page that handles sessions must have session_start() at the top, even if you are only reading sessions. This does not apply for includes!

Link to comment
Share on other sites

Are you redirecting right after you initially start the session? Whenever you first start the session you won't want to use a header redirect away from that page, some browsers will ignore the session cookie and just redirect. If you just started the session you would want to use a meta refresh to redirect, not a header.

Link to comment
Share on other sites

Hey, still wont work... So :

<?phpsession_start();include"system/db_connect.php";if(isset($_POST['loginn'])){$brukernavn=$_POST[brukernavn];$password=$_POST[passord];$checkbruker=mysql_query("SELECT brukernavn FROM brukere WHERE brukernavn='$brukernavn'");$checkbrukerO=mysql_num_rows($checkbruker);if($checkbrukerO == "0"){echo"Brukernavnet finnes ikke i vår database";exit();}$checkpass=mysql_query("SELECT * FROM brukere WHERE brukernavn='$brukernavn'") or die("Error : " .mysql_error()); $checkpassO=mysql_fetch_object($checkpass);if($password != "$checkpassO->passord"){ echo"Passordet du har skrevet inn er feil";exit();}else{$brukernavn=mysql_fetch_object(mysql_query("SELECT brukernavn FROM brukere WHERE brukernavn='$brukernavn'"));$brukernavno=$brukernavn;session_register($brukernavno);echo"<meta http-equiv=\"refresh\" content=\"0;URL=statestikk.php\">";}}?><head><title>Viva la PARKOUR!!!!!!!</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">body ,th ,td {	color:666666;	font-size:12px;	font-family:Arial, Helvetica, sans-serif;}</style></head>

Hope you can see the problem =)..

Link to comment
Share on other sites

Is that all the code? The HTML isn't complete, it's missing a body and there are no <html> tags, and there's no meta refresh tag. The refresh tag you are outputting is not going inside the head of the document, so I don't know what the browser is going to do with that.

Link to comment
Share on other sites

Is that all the code? The HTML isn't complete, it's missing a body and there are no <html> tags, and there's no meta refresh tag. The refresh tag you are outputting is not going inside the head of the document, so I don't know what the browser is going to do with that.
the other login file is a compleate html side, but it's a from action code to move it over... the <meta> is inside the php, the html code was just to make it able to have style, and it does refresh its just the session_registrer(); that is not able to work...
Link to comment
Share on other sites

Don't use session_register, that is the old way of doing it. In order to get it to work you need to have session_register before you work with the variable. But instead of doing this:session_register("var");$var = "value";just do this:$_SESSION['var'] = $value;then you don't need to use session_register at all. All the session_register function does is make an entry in the $_SESSION array, but it has other issues that affect it, so you might as well just use the $_SESSION array directly.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...