umer khayam
umer khayam

Reputation: 3

PHP session variables are not passing to next page

I am using this code to retrieve php session variables.

session_start();
$username = $_SESSION['username1'];
$password = $_SESSION['password1'];
echo $username;

When I execute it, I get the following error.

Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at /home/practice/public_html/wp-content/themes/twentytwelve/header.php:13) in /home/practice/public_html/wp-content/plugins/php-execution-plugin/includes/class.php_execution.php(273) : eval()’d code on line 2

If I remove session_start(), I get no error.

But in both cases, the session variable is not retrieved and the command echo $username; doesn't show anything.

Upvotes: 0

Views: 2762

Answers (3)

napolux
napolux

Reputation: 16114

I would not use sessions inside a wordpress installation. The code is too dirty to rely on the "no output".

Can't you use users table in wordpress as credential and authentication method?

Upvotes: 0

Ian Atkin
Ian Atkin

Reputation: 6366

You have something being output on the page before the session_start();. Make sure that the session_start(); comes before any echo/print statements, or HTML.

Your script is outputting something at line 13 in header.php. Put session_start(); before that, if not on the first line of the script.

When you remove session_start(); the session retrieval isn't attempted and the problem (that caused the error) goes away. PHP doesn't care that the variables you're trying to output don't exist. That's just how PHP is (though it will generate a Notice).

Upvotes: 1

Tobias
Tobias

Reputation: 1682

The error message already tells you, that you've ouput something before the session_start call:

output started at /home/practice/public_html/wp-content/themes/twentytwelve/header.php:13

The output is in the header.php file in line 13.

You have to start the session before any output to the client is sent.

Upvotes: 0

Related Questions