Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
sessionstart() and header error
Old 08-20-2009, 11:06 AM sessionstart() and header error
Average Talker

Posts: 27
Trades: 0
Ok so I recently learned php, and I am kinda getting used to it and it seems pretty nice. So I recently decided to add on a forum to my website, but the problem is that as soon as user logs in and my thing verifies that user exists i want to use session start, as soon as I do that I get this error
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at public_html/forum/verification.php:9) in public_html/forum/verification.php on line 56
And right after it this oneWarning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at public_html/forum/verification.php:9) in public_html/forum/verification.php on line 56
And then as soon as user logs in and the verification says he exists I want to redirect him to the forum page but I get this error
Warning: Cannot modify header information - headers already sent by (output started at public_html/forum/verification.php:9) in public_html/forum/verification.php on line 58

I have no idea what is wrong and tried many things, can someone please help me out and explain what I do wrong here?

This is the code by the way

$result = mysql_query($SQL);

if($result)
{
$num_rows = mysql_num_rows($result);

if($num_rows>0)
{
if ($num_rows == 1)
{
$errorMessage = "Logged on";
session_start();
$_SESSION['login'] = "1";
header ("Location: page1.php");
}
else
{
$errorMessage = "There are multiple users with same name";
}
}
else
{
$errorMessage = "Invalid logon";
session_start();
$_SESSION['login'] = '';
}
}
else
{
$errorMessage = "Error logging on";
}


Thanks a lot in advance to whoever helps me
Cinatas is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-20-2009, 11:31 AM Re: sessionstart() and header error
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
As the errors tells you, both the session_start() and the header() functions cannot be used after a header has been sent. What that means is, they can't be used after you've put out anything on the page.

So, make sure you have not printed ANY text on the page, including <html> tag, doctype or anything with echo or print etc. Also, make sure there aren't any line brakes or spaces before the <?php opening tag.

EDIT:
By the way, you can delete session variables with the session_unregister() function, or with session_destroy() to remove all variables and end the session, rather than setting their values to "".
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366

Last edited by lizciz; 08-20-2009 at 11:33 AM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-20-2009, 06:57 PM Re: sessionstart() and header error
Average Talker

Posts: 27
Trades: 0
Thanks a lot a redirect to the page worked just fine and sessionstart worked also, but now in the next page I was trying to use sessionstart also
Ok so if I cannot use sessionstart at all, then what is the use of it.
I mean there must be cases when you must use session start with a login page, because if you go to another page on the forum then how would you know if you are still there?
So after logging in I redirect user to the page1.php the code for it is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != ''))
{
header ("Location: login.php");
}
print "Yeah successful login";
?>
<body>
</body>
</html>

what would be the point of this page then if session start is useless? Because I have to use that info to know whether or not user is logged in.
Is there any other way I can do it?
Cinatas is offline
Reply With Quote
View Public Profile
 
Old 08-20-2009, 07:12 PM Re: sessionstart() and header error
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
So, make sure you have not printed ANY text on the page, including <html> tag, doctype or anything with echo or print etc. Also, make sure there aren't any line brakes or spaces before the <?php opening tag.
I'm sorry I explained it a bit clumsy. What I meant was, make sure you have not printed anything before calling the session_start() function. Once you've called the function you can output any code you want, as usual.

So, in short, put this first on your page, before the doctype
PHP Code:
<?php
   session_start
();
?>
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-20-2009, 07:56 PM Re: sessionstart() and header error
Average Talker

Posts: 27
Trades: 0
Awesome it works super fine now, thanks a lot.
Cinatas is offline
Reply With Quote
View Public Profile
 
Old 08-20-2009, 09:17 PM Re: sessionstart() and header error
Average Talker

Posts: 27
Trades: 0
Actually while messing with it more it works and doesn't at the same time.
The normal sessionstart works as long as user enters correct info, as soon as user enters incorrect info my program gets stuck on the page that verifies passwords and ids, so I decided to add a redirect. What I did is I closed my php code ?>
And then right after this I decided to add

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
header ("Location: http://studentindex.ca/student_index/intro.php");
?>
<body>
</body>
</html>

so as soon as I did that and ran the page entering incorrect info all I got is that same error I got previously. So I thought maybe I am doing something wrong and I decided to add another redirect link to the page that you get redirected to as soon as you login. So then I did this

<?php

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != ''))
{
header ("Location: login.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
print "Yeah successful login";
header ("Location: http://studentindex.ca/student_index/intro.php");
?>
<body>
</body>
</html>

as soon as I added that redirect command I got a problem. It would display Yeah sucessfull login just fine but then it would say
Warning: Cannot modify header information - headers already sent by (output started at public_html/forum/page1.php:16) in public_html/forum/page1.php on line 18

So I don't understand what did I do wrong this time?

If you want I can show you the full code I use for verifying the login info

Last edited by Cinatas; 08-20-2009 at 09:19 PM..
Cinatas is offline
Reply With Quote
View Public Profile
 
Old 08-20-2009, 09:52 PM Re: sessionstart() and header error
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
You can't use the header function if data has already been sent to the browser. Even if you could, the call to header you make would redirect to another page so there is no point to having any output at all. If you want to display a successful login page and then redirect to another page you'll need to use javascript.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-20-2009, 09:53 PM Re: sessionstart() and header error
rkuwadia's Avatar
Average Talker

Posts: 14
Name: Ravi Kuwadia
Trades: 0
Use buffering to avoid the header sent error. Its been a while since I used php but buffering is the solution.

Check http://php-programming.suite101.com/..._buffer_in_php for more information.

You can also google "php buffer start" to get more information.

With buffering the data is held on server before it is sent to client and you dont run into this error.
__________________
Ravi Kuwadia -
Please login or register to view this content. Registration is FREE
rkuwadia is offline
Reply With Quote
View Public Profile
 
Old 08-20-2009, 11:21 PM Re: sessionstart() and header error
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by rkuwadia View Post
Use buffering to avoid the header sent error. Its been a while since I used php but buffering is the solution.

Check http://php-programming.suite101.com/..._buffer_in_php for more information.

You can also google "php buffer start" to get more information.

With buffering the data is held on server before it is sent to client and you dont run into this error.
That's true, but in this case there is no point to having output at all since the call to the header function will cause a redirect. In other words, output buffering is not the solution here.

In any case, the documentation for output buffering can be found here:
http://us2.php.net/manual/en/ref.outcontrol.php
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-21-2009 at 01:22 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-21-2009, 12:59 AM Re: sessionstart() and header error
Average Talker

Posts: 27
Trades: 0
Thanks a lot guys it was very helpfull i finally got it working
__________________
Those who can: learn. Those who can't: teach.
Cinatas is offline
Reply With Quote
View Public Profile
 
Old 08-21-2009, 06:14 AM Re: sessionstart() and header error
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I've never had to use buffering in any of my sites. In absolutely most cases you can run almost all php code first; do logins, get database results, some other calculations or what ever. When you're done with that you'll know if you want to display the page or redirect. So you can do all the php work before outputting any code to the browser, thereby not getting the headers already sent warning.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to sessionstart() and header error
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.76466 seconds with 12 queries