开发者

First time using $_SESSION in PHP and I think I'm doing things wrong. I'm getting a syntax error

开发者 https://www.devze.com 2022-12-21 05:41 出处:网络
Here\'s my code for registerFormOne.php: <html> <head> <title>Registration Form - 1 of 2</title>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号