/    Sign up×
Community /Pin to ProfileBookmark

Session variable problems

Hi!

I create a session variable with a value into one HTML-page and then I want to open another HTML-page, but despite that I can’t get any value from the variable? I have use this code so far:

$control_status = “OK”;
session_register (“control”);
$HTTP_SESSION_VARS [“control”] = $control_status;

or

session_register (“test”);
$test = “Status OK”;

And together with this code before the start of the HTML code:

<?php
session_start();
?>

Must the the register_globals have to be on to use session variable?

I appreciate som help to create the best PHP-code. Thanks! ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 09.2009 — register_globals does not need to be enabled to use $_SESSION variables. Also, there's no need to use session_register and $HTTP_SESSION_VARS unless your PHP version is old.

Try this:

1st HTML Page"
[code=php]
<?php
session_start();
$_SESSION['test_var'] = "Testing";
?>
<!-- DOCTYPE, HTML, and CONTENT go here -->
[/code]



[code=php]
<?php
session_start();
?>
<!-- DOCTYPE, HTML, and CONTENT go here -->
<?php
echo $_SESSION['test_var'];
?>
[/code]


First simple rule of sessions is that session_start() must be declared on each page that uses any session variable AND session_start() must be executed before anything is sent to the browser:

This is right:

<i>
</i>&lt;?php
session_start();
?&gt;
&lt;!-- DOCTYPE, HTML, and CONTENT go here --&gt;


These are wrong:

[code=php]
<?php session_start(); ?>
<!-- DOCTYPE, HTML, and CONTENT go here -->
[/code]


[code=php]
<!-- DOCTYPE, HTML, and CONTENT go here -->
<?php session_start(); ?>
[/code]


Good luck and remember that php.net is your friend (session on php.net)
Copy linkTweet thisAlerts:
@pkngauthorApr 09.2009 — Thanks! You gave me a push forward! ?
×

Success!

Help @pkng 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...