max_w1 Posted January 31, 2008 Share Posted January 31, 2008 hi, i have a login page which looks like this if(isset($remember)) { setcookie('username', $username, time()+1209600); setcookie('password', base64_encode ($password), 0); setcookie('remember', $remember, 0); setcookie('ipaddress', $ipaddress, 0); session_start(); //remove the anti-hacking cookie setcookie ('tries', '', time()-60, '/', '', 0); $_SESSION['loggedin'] = $_POST['username']; $_SESSION['time'] = time(); header ('Location: /members.php'); echo("hi $username you are now logged in"); header("Location: members.php"); $loggedsuck = TRUE; } else { session_start(); //remove the anti-hacking cookie setcookie ('tries', '', time()-60, '/', '', 0); $_SESSION['loggedin'] = $_POST['username']; $_SESSION['time'] = time(); header ('Location: members.php'); } } i dont have any problem with my login page but when it redirects to memebers.php i am unable to echo session. i am using the following code in my memebers.php page: <?php $welcome = $_SESSION['loggedin']; echo "$welcome"; ?> i will be glad to receve any help! Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 31, 2008 Share Posted January 31, 2008 session_start(); must be placed before any code. eg <?php session_start(); do the rest of the code ?> try that Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 <?php session_start(); $welcome = $_SESSION['loggedin']; echo "$welcome"; ?></div> i did that buy it does not work. i think there is a problem in the code which sets session. one more thing... there is a index.html page which POSTs data to login.php and login.php sets the session and redirects us to members.php, so can members.php display session? ??? if yes please help me!! thankyou in advance Quote Link to comment Share on other sites More sharing options...
haku Posted January 31, 2008 Share Posted January 31, 2008 Did you set the code kingy told you at the top of both pages? Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 yes i did, i noticed one more thing!,, i am able to read session from login.php page but not able to read it from members.php page Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 Your session_start on the first page is after your cookies, move it to the top. Also, all those cookies that you are setting that doesn't have a time on it, will expire once you close the browser. Not sure if you know that or not. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 i tried moving it to top. and as i said before... i am able to display sessions from login.php page but unable to display it from members.php page. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 You probably have whitespace before the session_start() Check the code with Notepad. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 there is no white space the code looks like this: <?php ob_start(); session_start(); ///////////////////////////////////////////////////////////////////////////////////////////// /**//**//**/require ("config.php");/**//**//**//**//**//**//**//**//**//**//**//**//**//**/// ///////////////////////////////////////////////////////////////////////////////////////////// if (isset ($_COOKIE['password']) || isset ($_COOKIE['rempassword'])) { echo("you are already logged in"); } else { Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 session_start(); has to be the First line after <?php Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 noops :-\... its not working. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 i think there is a problem with my local server... i think it does not support sessions. thats why they are getting destroyed as soon as i leave the page. can i use cookies insted of using sessions??? because cookies are working perfectly? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 Check the php.ini for the session save path Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 31, 2008 Share Posted January 31, 2008 on login.php but the following: <?php session_start(); print "SESSION ID: ".session_id(); print "\nSession module: ".session_module_name(); print "\nSession save path: ".session_save_path(); $_SESSION['loggedin'] = 'testuser'; $_SESSION['time'] = time(); print "\nSession data: "; print_r($_SESSION); ?> and on members.php put: <?php session_start(); print "SESSION ID: ".session_id(); print "\nSession data: "; print_r($_SESSION); ?> and let us know what happens Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 31, 2008 Share Posted January 31, 2008 Note: There is no header() forward in there, so just navigate your browser to login.php and then navigate to members.php Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 i tried using those codes but nothing happens ... i am still not able to view session... what to do.. please help!!! Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 Read my post above what to do.. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 yes login.php says: SESSION ID: fdfd98a60c5942fb5b4b07fbb933795d Session module: files Session save path: C:\Program Files\YellowTip/tmp Session data: Array ( [loggedin] => testuser [time] => 1201804153 ) and members.php says: SESSION ID: fdfd98a60c5942fb5b4b07fbb933795d Session data: Array ( [loggedin] => testuser [time] => 1201804153 ) what does that mean?? i dont understand.... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 31, 2008 Share Posted January 31, 2008 that is a very good thing... how are you submitting the user/password data to login.php? try this on login.php, and leave members.php alone for now <?php session_start(); if(isset($remember)){ setcookie('username', $username, time()+1209600); setcookie('password', base64_encode ($password), 0); setcookie('remember', $remember, 0); setcookie('ipaddress', $ipaddress, 0); } //remove the anti-hacking cookie setcookie ('tries', '', time()-60, '/', '', 0); $_SESSION['loggedin'] = $_POST['username']; $_SESSION['time'] = time(); header ('Location: /members.php'); exit; ?> Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 it's still not displaying the session in the members.php. i am using index.htm to submit data to login.php. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 Read my post above what to do.. how am i suppose to find it? php.ini is a very long page... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 31, 2008 Share Posted January 31, 2008 I assume the code we are discussing for login.php is only a snippet. Can you paste the entire file? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 31, 2008 Share Posted January 31, 2008 .htm won't parse .php by default. change the name to index.php it's still not displaying the session in the members.php. i am using index.htm to submit data to login.php. Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 the entire login.php looks like this <?php ob_start(); print "SESSION ID: ".session_id(); print "\nSession module: ".session_module_name(); print "\nSession save path: ".session_save_path(); $_SESSION['loggedin'] = 'testuser'; $_SESSION['time'] = time(); print "\nSession data: "; print_r($_SESSION); //////////////////////////////////////////////////////////////////////////////////////////// /**//**//**/require ("config.php");/**//**//**//**//**//**//**//**//**//**//**//**//**//**/// ///////////////////////////////////////////////////////////////////////////////////////////// if (isset ($_COOKIE['password']) || isset ($_COOKIE['rempassword'])) { echo("you are already logged in"); } else { // Variables that data come from the form $username = $_POST["username"]; $password = $_POST["password"]; $login = $_POST["login"]; $ipaddress = $REMOTE_ADDR; $remember = $_POST["remem"]; // Check if username and password where submitted if(isset($login)) { if(isset($login)) { if (!$username) { echo "Please enter username"; exit; } if (!$password) { echo "Please enter password"; exit; } $issuchusername = mysql_query("SELECT * FROM users WHERE username = '$username'"); $usernamelogin = mysql_num_rows($issuchusername); if ($usernamelogin == 1) { $issuchpassword = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); $passwordlogin = mysql_num_rows($issuchpassword); if ($passwordlogin == 1) { if(isset($remember)) { if(isset($remember)){ setcookie('username', $username, time()+1209600); setcookie('password', base64_encode ($password), 0); setcookie('remember', $remember, 0); setcookie('ipaddress', $ipaddress, 0); } //remove the anti-hacking cookie setcookie ('tries', '', time()-60, '/', '', 0); $_SESSION['loggedin'] = $_POST['username']; $_SESSION['time'] = time(); header ('Location: /members.php'); exit; } else { session_start(); //remove the anti-hacking cookie setcookie ('tries', '', time()-60, '/', '', 0); $_SESSION['loggedin'] = $_POST['username']; $_SESSION['time'] = time(); header ('Location: members.php'); } } } else { echo "Incorrect username/password1"; header("Location: index.php?error=true"); exit; } } if(!$usernamelogin) { echo "incorrect username or password"; header("Location: index.php?error=true"); exit; } } } ?> Quote Link to comment Share on other sites More sharing options...
max_w1 Posted January 31, 2008 Author Share Posted January 31, 2008 .htm won't parse .php by default. change the name to index.php :(i did it.. now index.htm is index.php but its giving me the same problem. Quote Link to comment 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.