Here's my code for registerFormOne.php:
<html>
<head>
<title>Registration Form - 1 of 2</title>
</head>
<body>
<h1>Registration - Part 1 of 2</h1>
<p>Please fill in all the required information before submitting the information.</p>
<form action="registerFormTwo.php" method="post">
First Name:<input type="text" name="firstName" /><br /><?php session_start(); $_SESSION['firstName'] = firstName; ?>
Last Name:<input type="text" name="firstName" /><br /><?php se开发者_如何学Cssion_start(); $_SESSION['firstName'] = firstName; ?>
Age:<input type="text" name="firstName" /><br /><?php session_start(); $_SESSION['firstName'] = firstName; ?>
Date of Birth:<input type="text" name="firstName" /><br /><?php session_start(); $_SESSION['firstName'] = firstName; ?>
<input type="submit" />
</form>
</body>
Here's the error I'm getting:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\XAMPP\xampp\htdocs\registerFormOne.php:10) in C:\XAMPP\xampp\htdocs\registerFormOne.php on line 10
What could be the error? Also, I'm not sure where I'm supposed to save a value to a SESSION. In ASP, I would put it in the btn_Click method; but here in PHP it's a whole other world.
Thanks a bunch guys. :)
First of all session_start() causes a cookie to be sent to the browser - it needs to be called before you've sent ANY OTHER output. Because you are outputting HTML before you make the call, you're getting the error.
Secondly, you only need to call it once.
You only need to call session_start()
once. Also, you seem to be confusing what's happening on the client versus what's happening on the server. If you have form elements, you need to process them on the server (via either $_GET
or $_POST
depending on your form type) and then assign them to the session.
This error is caused because you are sending text output to the browser before you call session_start(); , session_start() needs to be called before any tex tis outputted.
Also you only need to call session_start() once to use sessions
I think you need to have a good look through how sessions work, A website that explains the basics well is here - http://www.tizag.com/phpT/phpsessions.php
Also you are submitting data using a form you do not need to use sessions for that - You can use post data which will work fine.
As people have mentioned session_start() should be called before any HTML is outputted.
<?php session_start() ?>
<html>
etc...
The bigger problem is also that you are calling
<?php $_SESSION['something'] ?>
which will do absolutely nothing.
The proper use is to create your form then in the php file that is your application do something like this
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// Tell the server to start capturing session data.
session_start();
// set the hash key for first_name
$_SESSION['first_name'] = $_POST['first_name'];
// retrieves the first_name hash key for the session array.
print $_SESSION['first_name'];
}
Before you can set or access the $_SESSION you have to start it, unless session autostart is setup up on the server.
精彩评论