I am building my tutorial website. For which i am having login facility. When the session is started my homepage opens. I going to have multiple webpages, when i click the link the session must continue to the next consecutive pages too. And so the variable username at the top most left corner remains until the session expires(when pressing logout). How to do it? please help me.
-
Consider selecting one of the answers as the correct answer if your query has been solved. – LoneWOLFs Apr 04 '13 at 05:02
4 Answers
try this
if(isset($_SESSION['your_sess_name']) && !empty($_SESSION['your_sess_name'])) {
echo 'your session exists';
}
to logout
unset($_SESSION['your_sess_name']);
session_destroy();

- 5,814
- 3
- 24
- 33
-
just using session_start(); at the top of th page is more than enough for this question. I found the answer, thank you. – Franklin Vaz Apr 03 '13 at 12:46
-
-
-
its redundant to use !empty and isset. See http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty – Adam Mar 24 '17 at 11:23
Include session_start();
as the first line in each of the webpages where you require the session. Its not good to open it on each and every page.
Also to check if session is open or not you can include the following code
<?php
if(strlen(sessionid()) > 0){
//session exists
echo "Session Started!";
}else{
echo "No Session Found!";
}
?>
You can also include the above code in a single file and require it on pages where you need the session.

- 2,306
- 3
- 20
- 38
-
thats a simple answer right? i think your answer helped me.. thank you LoneWOLFs – Franklin Vaz Apr 03 '13 at 12:45
SESSION is Non-Persistent cookie, it will be available on all pages till the browser closed. The maximum age of SESSION depends how long you stay on that session, once you closed the browser window, the session will be terminated by browser.
Simply, on top of all PHP file: you can call:
<?php
session_start();
if(isset($_SESSION['name']) && $_SESSION['name'] == 'some value')){
}
?>
first create one file as config.php
. in this file write your condition like this one:
session_start();
if((!isset($_SESSION['session_name'])) and (!isset($_GET['lgact'])))
{
header("location:".SITE_URL."index.php");
exit;
}
and then just include this file in your webpages.
include("config.php");
hope this is what you find.

- 2,995
- 4
- 52
- 102