kks_krishna Posted July 7, 2007 Share Posted July 7, 2007 HI, i want to implement session in my site. If for a particular page should be accesses by only the loged in user. so I need to check whether the user is logged in or I shoud forward to Registration page or login page. Once the user is logged in we can use the userid to log the activities. How can we do this? Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/ Share on other sites More sharing options...
tamumech Posted July 7, 2007 Share Posted July 7, 2007 When the user logs in, set $_SESSION['auth'] to TRUE or whatever. Then on each page check it like this: <?php session_start(); if ( @$_SESSION['auth'] != "TRUE" ) { header("Location: login.php"); die(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292006 Share on other sites More sharing options...
kks_krishna Posted July 7, 2007 Author Share Posted July 7, 2007 Thank you buddy. Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292008 Share on other sites More sharing options...
wildteen88 Posted July 7, 2007 Share Posted July 7, 2007 Don't use @ for suppressing errors. If you are checking input variables (_GET, _POST, _COOKIE, _SESSION etc) values always use isset function and then check it's value. So instead of: if ( @$_SESSION['auth'] != "TRUE" ) Do: if ( isset($_SESSION['auth']) && $_SESSION['auth'] != "TRUE" ) @ should only be used as a last resort. Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292042 Share on other sites More sharing options...
kks_krishna Posted July 7, 2007 Author Share Posted July 7, 2007 How to set set $_SESSION['auth'] to TRUE. Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292172 Share on other sites More sharing options...
sinisake Posted July 7, 2007 Share Posted July 7, 2007 I guess you have login form? Check DB fileds for username and password and if user exists, set $_SESSION['auth']=true;. Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292176 Share on other sites More sharing options...
cooldude832 Posted July 7, 2007 Share Posted July 7, 2007 its just like assigning any other variable, but it must be done pre headers and you have to have session_start(); at the top of the page like $_SESSION[string or var] = "value"; Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292179 Share on other sites More sharing options...
kks_krishna Posted July 8, 2007 Author Share Posted July 8, 2007 Its working fine for me. How can I terminate the session when user clicks on the LogOut button. Also guide me how to store the user login into cockies so that when user comes next time it should automatically logged in. Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292192 Share on other sites More sharing options...
kks_krishna Posted July 8, 2007 Author Share Posted July 8, 2007 How to reterive the values from session Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292194 Share on other sites More sharing options...
cooldude832 Posted July 8, 2007 Share Posted July 8, 2007 same way $var = $_SESSION[sesionvar]; Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292204 Share on other sites More sharing options...
kks_krishna Posted July 8, 2007 Author Share Posted July 8, 2007 HI, What is the difference between using print and echo. Normally for printing I am using print() method. what is the use of echo(). Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292213 Share on other sites More sharing options...
kks_krishna Posted July 8, 2007 Author Share Posted July 8, 2007 Please check the code whether its correct : <?php include("UsersOperations.php"); $user = new UsersOperations; $user_name = $_GET['user_name']; $pass = $_GET['pass']; $result = $user->check_user($user_name,$pass); if($result==1) { session_start(); $_SESSION['auth'] = TRUE; $_SESSION['user_name'] = $user_name; header("Location: ../index.php"); die(); } ?> <?php $var = $_SESSION['user_name']; print($var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292219 Share on other sites More sharing options...
sinisake Posted July 8, 2007 Share Posted July 8, 2007 session_start(); -MUST BE ON THE TOP OF THE CODE, otherwise it wouldn't work. you mustn't have ANY other output before starting session... Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292220 Share on other sites More sharing options...
kks_krishna Posted July 8, 2007 Author Share Posted July 8, 2007 Thanks. How to terminate the session when clicking on logout? Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292235 Share on other sites More sharing options...
sinisake Posted July 8, 2007 Share Posted July 8, 2007 http://php.net/session_unset Quote Link to comment https://forums.phpfreaks.com/topic/58851-help-needed-on-php-session/#findComment-292238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.