/    Sign up×
Community /Pin to ProfileBookmark

Error while Creating Session in PHP .

I have installed XAMP . Now when I am trying to create a session using following code

<?php
session_start();
$_SESSION[‘username’] = ‘abcde’;
$_
SESSION[‘role’] = ‘admin’;
?>

I get following erroer

Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at C:xampphtdocstrialSES1.PHP:3) in C:xampphtdocstrialSES1.PHP on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:xampphtdocstrialSES1.PHP:3) in C:xampphtdocstrialSES1.PHP on line 4

The php ini has following

session.save_path = “C:xampptmp”

Could you guide me .!! Need urgent help

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@EJMAES1973Sep 15.2008 — Do you have anything above those lines in your file?
Copy linkTweet thisAlerts:
@svidgenSep 15.2008 — Here's the key phrase:
... Cannot send session cookie - headers already sent by ...[/QUOTE]
So, you've either got to use output buffering, or you need to make sure your call to [I]session_start()[/I] is before your script sends anything to the browser, because that will trigger sending the HTTP headers. Once the headers are sent, a cookie header cannot be sent by the server, which prevents PHP from starting the session. PHP knows this, so it complains if you try. Hence the warnings.

Does that make sense?
Copy linkTweet thisAlerts:
@kiwi2Sep 15.2008 — as far as I know sessions do not work on the xampp simulate

environment, test your code online, remember to declare session_start() at the very top of your page, and if using php 5 and above call session_register, after session_start.I have installed XAMP . Now when I am trying to create a session using following code

<?php

session_start();

$_SESSION['username'] = 'abcde';

$_
SESSION['role'] = 'admin';

?>

I get following erroer

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:xampphtdocstrialSES1.PHP:3) in C:xampphtdocstrialSES1.PHP on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:xampphtdocstrialSES1.PHP:3) in C:xampphtdocstrialSES1.PHP on line 4


The php ini has following


session.save_path = "C:xampptmp"

Could you guide me .!! Need urgent help[/QUOTE]
Copy linkTweet thisAlerts:
@svidgenSep 15.2008 — as far as I know sessions do not work on the xampp simulate environment[/QUOTE]
That would be pretty shocking (to me, anyway). Are you sure?
Copy linkTweet thisAlerts:
@NogDogSep 15.2008 — as far as I know sessions do not work on the xampp simulate

environment,[/quote]

How so? It installs Apache, PHP, and MySQL (and Perl). How is that a "simulated" environment"? Anyway, I've used sessions with no problems in an XAMPP installation.
...test your code online, remember to declare session_start() at the very top of your page, and if using php 5 and above call session_register, after session_start.[/QUOTE]
session_register() is deprecated and should be avoided unless you are still depending on the also deprecated register_globals behavior; and if you are, it's time to stop and get with the current best practices before PHP6 comes out and your code is worse than deprecated (i.e.: unusable).
×

Success!

Help @arpit_vavadia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...