...

View Full Version : Resolved Session Start and check login



masterofollies
02-27-2009, 02:03 AM
Hi, I want to add "Welcome Back $user" to my index.php page. It's not working I get the following error.


Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/rodgameo/public_html/projects/dmv/index.php:1) in /home/rodgameo/public_html/projects/dmv/index.php on line 35

I figure this is because SESSION_START needs to be on line one. But it still isn't working.

This is the very top of my page.


<?php
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
?>

and this is in the script in the spot where I want to check.


<li>
<?php
session_start();
if(!empty($_SESSION["user_id"])){
$who = mysql_query("SELECT username FROM users WHERE id='".$_SESSION['user_id']."'");
$row = mysql_fetch_array($who);
echo "<b>Welcome Back ".$row['username']."!</b>";
}


?>
</li>

How can I get it to check the session and pull the username from the database both?

PappaJohn
02-27-2009, 02:05 AM
It's because of the html output before the call to session_start().

Move session_start() to the very top of the file.

masterofollies
02-27-2009, 02:29 AM
I tried and it still didn't fix it.
I put it right after the <?php

Fou-Lu
02-27-2009, 02:40 AM
Are you including this file into a file that has previous output as well?
The same rules apply, session_start needs to be called prior to any output to the browser, otherwise you'll need to use output buffering which I don't really recommend for these types of purposes.

masterofollies
02-27-2009, 02:44 AM
No include files, straight html other then this part.

Fou-Lu
02-27-2009, 02:46 AM
When you removed the <li>, did you also remove the whitespace before the <?php as well? The whitespace is also considered output.

masterofollies
02-27-2009, 02:49 AM
No the <li> is in the body. This is what the top looked like.



<?php
session_start();
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>East Coast Entrepreneurs</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>

Fou-Lu
02-27-2009, 02:54 AM
Ok yeah, and this code here:


<li>
<?php
session_start();
if(!empty($_SESSION["user_id"])){
$who = mysql_query("SELECT username FROM users WHERE id='".$_SESSION['user_id']."'");
$row = mysql_fetch_array($who);
echo "<b>Welcome Back ".$row['username']."!</b>";
}


?>
</li>

Is being included further into the script you just posted correct? In that case, you don't need the session_start() in the code I just posted since it already exists from the parent page scope.

masterofollies
02-27-2009, 03:11 AM
Yeah its about halfway down the script in the menu bar. So just don't use session_start and it should be golden?

CFMaBiSmAd
02-27-2009, 03:29 AM
You are working in index.php and from the error - output started at /home/rodgameo/public_html/projects/dmv/index.php:1

If there is nothing in your source code before the <?php tag, your file is probably saved in UTF-8 encoding and the BOM (Byte Order Mark) characters are the content being output on line 1.

Save your file as ANSII/ASCII or if you must save it as UTF-8 encoding, save it without the BOM.

masterofollies
02-27-2009, 03:49 AM
I was wondering what those were. I accidently changed it and didn't know what it used to be so I thought UTF-8 would be a good choice, but I guess not. I changed to ANSI.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum