0

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.

1
  • Is display_errors on? else ini_set('display_errors', '1'); You might see what the error is. Commented Jan 27, 2013 at 8:16

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

if i remove the session_start() and put it at the top, the error removes but i still dont get the session variables.
@umerkhayam I explain why in my answer.
0

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.

1 Comment

thank you. solved the problem. i put the session start () at the top of the header.php.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.