Reputation: 2066
This is the first time I'm learning sessions with php and I'm working on the most simple login page possible.
So could this theoretically keep any given string $username always stored in $_SESSION['username']
so long as the user never closes out of the web page?
This is a dummy web page that simply sets the value of $_SESSION['username']
to $username. If I do this, and I open a different page on my website, will the value of $_SESSION['username']
still be retained until I close the web browser?
<?php
session_start();
<html>
<body>
...
$_SESSION['username'] = $username;
?>
So my big question is, if $username was "CiniCraft" will $_SESSION['username'] = "CiniCraft" until the web browser is closed? If not, what's the best variable to use that retains its value until the browser is closed?
I've tried the approach shown above, the $_SESSION['username'] becomes blank when I navigate from login.php to index.php.
Security is not an issue for me at all, if user accounts become compromised then hackers will be wasting their time. Unless of course they're looking for emails to spam more Viagra ads...
Upvotes: 0
Views: 2937
Reputation: 14275
Yes, whenever you do
$_SESSION['username'] = $username;
the session will be assigned the $username
value. However, remember to start the session before assigning it, so the order would be like this:
session_start();
$_SESSION['username'] = $username;
Start the session on every page where you need to access it - that is probably the reason why it was not available on index.php.
So on index.php you could do something like this:
session_start();
echo $_SESSION['username'];
This, in your example, should output "CiniCraft", given that you first call login.php and index.php afterwards.
The session is stored in your browser with a cookie - so as long as you don't reset your browser or close it for a longer time period, the session should be valid. In other browsers, however, the session will not be available.
Read more about sessions in php context and generally.
Security is not an issue for me at all
Careful - please don't look at it this way. Security is everyone's responsibility, and you will need it sooner rather than later.
Upvotes: 6
Reputation: 7918
It seems that you are using old browsers and thus request you to update the browser first. As in modern browser's the handling of sessions is easy than old browsers.
Another thing you have to provide session_start() on all pages at the starting point of the page.
session_start will help to maintain single session on across website when users are visiting your website.
Upvotes: 0