scottreid1974 Posted January 27, 2007 Share Posted January 27, 2007 Hi, i have a logon page where users enter a password, and user name, and they then get redirected to another web page. What i want is to make all the pages they visit when logged on secure, presumably using sessions. I am not sure of the code to use to create the sessions on the logon page, and the validation type sessions on the following visited pages.The logon page code is below.Thanks<?phpif (!isset($_SESSION)) { session_start();}$loginUsername = $_POST['username'];$password = $_POST['password'];mysql_select_db($database_testconn1, $testconn1);$sql = "SELECT username, password, url, access FROM users WHERE username='$loginUsername' AND password='$password' LIMIT 1";$user = mysql_fetch_assoc(mysql_query($sql, $testconn1));if(count($user) > 0){ header('Location: '.$user['url']);}else{ header('Location: login.php');}?> Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/ Share on other sites More sharing options...
taith Posted January 27, 2007 Share Posted January 27, 2007 scrap the "if (!isset($_SESSION)) {" session_start(); on its own will start a session, if its not already started... Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170460 Share on other sites More sharing options...
sayedsohail Posted January 27, 2007 Share Posted January 27, 2007 Example 8-2: Simple PHP script that uses a session http://www.oreilly.com/catalog/webdbapps/chapter/ch08.html#97915http://www.tizag.com/phpT/phpsessions.phpor try google php sessions Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170461 Share on other sites More sharing options...
scottreid1974 Posted January 27, 2007 Author Share Posted January 27, 2007 Tried the tutorials, but i still cant get it to work,The problem is, i can get sessons to work on one webpage, but i cant when trying to get it to work on a webpage retrieved from a database, when user logs in . The url is stored in a field in the database, and is specific to each log on (account)any help plllzzz Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170470 Share on other sites More sharing options...
Iceman512 Posted January 27, 2007 Share Posted January 27, 2007 Hi Scott,Each subsequent page that uses your session or session data will have to have a [code]session_start() [/code] at the very top of the page, before any other output is sent. For example:[code]<?phpsession_start() ?><html> <head><title>Any page on your site</title></head> <body> The page data. </body></html>[/code]Hope that helps you. Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170488 Share on other sites More sharing options...
kenrbnsn Posted January 27, 2007 Share Posted January 27, 2007 The "session_start()" must be present in all scripts where you want to use the session. Is it?Ken Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170489 Share on other sites More sharing options...
HuggieBear Posted January 27, 2007 Share Posted January 27, 2007 Scott,Your problem is that you aren't setting the session variables anywhere. As soon as your user is confirmed as being logged in, you're redirecting them to their homepage.This code:[code]<?phpif(count($user) > 0){ header('Location: '.$user['url']);}?>[/code]Should look more like this:[code]<?phpif(count($user) > 0){ $_SESSION['username'] = $user['username']; // This assigns the value to a session variable header('Location: '.$user['url']);}?>[/code]Check your email ;)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35938-cant-get-sessions-to-work/#findComment-170528 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.