Help with sessions.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Help with sessions.

Post by gkwhitworth »

Ok...I got the information to the next page using sessions, but I keep on getting errors.

My php code is as follows:

Code: Select all

<?

	session_start();
	session_register("email");
	session_register("name");
	session_register("phone");
	session_register("contact");
	session_register("comments");
	
	$email = $_REQUEST['email'];
	$name = $_REQUEST['name'];
	$phone = $_REQUEST['phone'];
	$contact = $_REQUEST['contact'];
	$comments = $_REQUEST['comments'];
	
	ini_set("SMTP","127.0.0.1");
	
       	mail("contact@gkwinspired.com", "Feedback Form Results" , "name: $name\nPhone: $phone\nContacting Who: $contact\nComments:\n$comments", 
		"From: $name <$email>");
		header ("Location: http://www.gkwinspired.com/feedback.php");	
	
?>
My php on the confirmation page is:

Code: Select all

<?php
	session_start();
		
	print("Thank you so much for sending us your feedback.<br><br>");
	print("<b>Name:</b> $name<br>");
	print("<b>Email:</b> $email<br>");
	print("<b>Comments:</b> $comments<br>");
	print("<b>Contact Who:</b> $contact<br>");
	print("<b>Phone:</b> $phone<br>");
	
?>

Code: Select all

The errors I am recieving is as follows:

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\users\gkwinspired.com\feedback.php:12) in D:\users\gkwinspired.com\feedback.php on line 83

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\users\gkwinspired.com\feedback.php:12) in D:\users\gkwinspired.com\feedback.php on line 83
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Couple of things wrong with all of that. One, stop using session_register(). Assign your session vars to the $_SESSION superglobal array. Next, stop treating your sessions vars as though register_globals is on. Next, and most importantly in answering your question, make sure there is nothing at all sent to the browser before your call to session_start().

As an aside, this problem crops up about once a week. Did you search the forum before posting?
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

ok...

Post by gkwhitworth »

Alright, I really am trying to do this right....

I want to place them all into an array as you said using the $_Session but how would that be accomplished? I just want to have one $_Session so this is what I have so far:

Code: Select all

<?
   blah blah
        $email = $_REQUEST['email'];
	$name = $_REQUEST['name'];
	$phone = $_REQUEST['phone'];
	$contact = $_REQUEST['contact'];
	$comments = $_REQUEST['comments'];
	
	$_SESSION["$email && $name && $phone && $contact && $comments"];
?>
I know this isn't correct because I get an error....how would I do this?

--
Greg

P.S. Thanks for the critisism, I am very very new to PHP.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$_SESSION['email'] = $_REQUEST['email'];
I wouldn't write that for each parameter but use an loop like

Code: Select all

foreach( array('email','name','phone','contact','comments') as $index) {
	$_SESSION[$index] = $_REQUEST[$index];
}
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Thanks

Post by gkwhitworth »

That is exactly what I am looking for....I am not too up with the syntax of PHP yet. I am now getting the following errors from the code though they are:

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\users\gkwinspired.com\feedback.php:12) in D:\users\gkwinspired.com\feedback.php on line 83

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\users\gkwinspired.com\feedback.php:12) in D:\users\gkwinspired.com\feedback.php on line 83
Here is the sendmail php code:

Code: Select all

<?

	session_start();
		
	$email = $_REQUEST['email'];
	$name = $_REQUEST['name'];
	$phone = $_REQUEST['phone'];
	$contact = $_REQUEST['contact'];
	$comments = $_REQUEST['comments'];
	
	foreach( array('email','name','phone','contact','comments') as $feedback) {
		$_SESSION[$feedback] = $_REQUEST[$feedback];
		}
			
	ini_set("SMTP","127.0.0.1");
	
       	mail("contact@gkwinspired.com", "Feedback Form Results" , "name: $name\nPhone: $phone\nContacting Who: $contact\nComments:\n$comments", 
		"From: $name <$email>");
		header ("Location: http://www.gkwinspired.com/feedback.php");	
	
?>

It seems I constantly have problems with the can't post "blah blah" because I have already sent stuff to the browser...I can't seem to get it down.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Everah wrote:Next, and most importantly in answering your question, make sure there is nothing at all sent to the browser before your call to session_start().
gkwhitworth wrote:output started at D:\users\gkwinspired.com\feedback.php:12
Something at line 12 generates output. What is line 12?
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

I can't seem to find anything on line 12 on any page, the form page, the php script, or the confirmation page.

Sorry. I tried moving the session_start(); to the top like this:

Code: Select all

<head>
<?php
	session_start();
?>
and I still get errors like this:

Code: Select all

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\users\gkwinspired.com\feedback.php:4) in D:\users\gkwinspired.com\feedback.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\users\gkwinspired.com\feedback.php:4) in D:\users\gkwinspired.com\feedback.php on line 5
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

just put session start at the top of the page, entire page goes below it like

Code: Select all

<?
//don't put even a space before session start
session_start();
<html>
<head>
//rest of the page
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

output started at D:\users\gkwinspired.com\feedback.php:4
Now it's line 4.
What is on line 4? It's generating output. php is never ever wrong about that.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

gkwhitworth wrote:

Code: Select all

<head>
<?php
	session_start();
?>
This is your problem. You sent the <head> tag to the browser before calling session start. You cannot send anything, not even a single white space, as output before your call to session_start() (or header() or setcookie()) as you will get this error message everytime.
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

Thanks for all the help guys....I got it to work with no errors and it displays the form information.

--
Greg
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Way to go man. Good job.
Post Reply