runnerjp Posted November 22, 2007 Share Posted November 22, 2007 <?php if ( $_SESSION['logged_in'] ){ ?> welcome member <?php endif; ?> here is the code to prevent users from viewing page. yes simple i know but this is not the problem for the life of me i cant do it lol what i want is an error message to show if the user is not logged in. i have tried adding <?php } else { ?> <p>This is the protected page. Your private content goes here.</p> <?php } ?> and verius other methords and know luck... i should now how to do this but its not there lol Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Try this... <?php if ($_SESSION['logged_in'] ) { echo "welcome member"; } else { echo "GET LOST"; } ?> Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted November 22, 2007 Share Posted November 22, 2007 <?php if (!$_SESSION['logged_in'] { echo "<p>This is the protected page. Your private content goes here.</p>"; }else{ echo "Welcome Member"; } ?> Not sure if that will work but give it a try and see. Stephen Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 thanks that worked but how can a add a large amaount on information onto it... will it all have to go in echo or could i write this on anouthe rpage and direct the echo to it Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 ok it does not work lol it always says get lost been logged in or out Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 This works as i have it in my working login script if (!isset($_SESSION['enter your session name here']) || $_SESSION[enter your session name here'] !== true) { // not logged in, move to login page header('Location: add your login script here); exit; } Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 <?php if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { // not logged in, move to login page header("Location: http://www.runningprofiles.com"); exit; } ?> i did it but been both logged in and loged out sends me to ("Location: http://www.runningprofiles.com"); Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 That script will stop people getting into http://www.runningprofiles.com if they are not logged in. If they are logged in then they can view any html code below the php. if not they are redirected to the login screen. Not sure whats going on. Send me the login code and i'll see if anything is wrong. I am new to php so bear with me. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 well this is what i usuall use to stop people logging in <?php if ( ! $_SESSION['logged_in'] ): ?> Content here <?php endif; ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 yeah that is pretty much the same as what i put. The extra bit is just an or statement incase the first check fails. No idea why its there to be honest. It was just in the tutorial i followed. If you dont want a re direct you can remove header("Location: http://www.runningprofiles.com"); and put echo blah blah blah; exit; I think that will work. Not 100% sure though Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 nope something aint right <?php if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { // not logged in, move to login page echo "your not allowed in" ; exit; } ?> see if you are not logged in and try accessing the page it reads the echo "your not allowed in" but when logging in you get redirected to the page it still reads "your not allowed in" thanks for help hard getting to grips with this stuff aint it lol Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 ok. Post The login form. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 here is login form <?php require_once ( 'settings.php' ); if ( array_key_exists ( '_submit_check', $_POST ) ) { if ( $_POST['username'] != '' && $_POST['password'] != '' ) { $query = 'SELECT ID, Username, Active, Password FROM ' . DBPREFIX . 'users WHERE Username = ' . $db->qstr ( $_POST['username'] ) . ' AND Password = ' . $db->qstr ( md5 ( $_POST['password'] ) ); if ( $db->RecordCount ( $query ) == 1 ) { $row = $db->getRow ( $query ); if ( $row->Active == 1 ) { set_login_sessions ( $row->ID, $row->Password, ( $_POST['remember'] ) ? TRUE : FALSE ); header ( "Location: " . REDIRECT_AFTER_LOGIN ); } elseif ( $row->Active == 0 ) { $error = 'Your membership was not activated. Please open the email that we sent and click on the activation link.'; } elseif ( $row->Active == 2 ) { $error = 'You are suspended!'; } } else { $error = 'Login failed!'; } } else { $error = 'Please use both your username and password to access your account'; } } ?> and sessions are function checkLogin ( $levels ) { session_start (); global $db; $kt = split ( ' ', $levels ); if ( ! $_SESSION['logged_in'] ) { $access = FALSE; if ( isset ( $_COOKIE['cookie_id'] ) ) {//if we have a cookie $query = 'SELECT * FROM ' . DBPREFIX . 'users WHERE ID = ' . $db->qstr ( $_COOKIE['cookie_id'] ); if ( $db->RecordCount ( $query ) == 1 ) {//only one user can match that query $row = $db->getRow ( $query ); //let's see if we pass the validation, no monkey business if ( $_COOKIE['authenticate'] == md5 ( getIP () . $row->Password . $_SERVER['USER_AGENT'] ) ) { //we set the sessions so we don't repeat this step over and over again $_SESSION['user_id'] = $row->ID; $_SESSION['logged_in'] = TRUE; //now we check the level access, we might not have the permission if ( in_array ( get_level_access ( $_SESSION['user_id'] ), $kt ) ) { //we do?! horray! $access = TRUE; } } } } } else { $access = FALSE; if ( in_array ( get_level_access ( $_SESSION['user_id'] ), $kt ) ) { $access = TRUE; } } if ( $access == FALSE ) { header ( "Location: " . REDIRECT_TO_LOGIN ); Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 Change in the login form header ( "Location: " . REDIRECT_AFTER_LOGIN ); to header('Location: add the page you want users to see'); Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 that didnt chnage anything as . REDIRECT_AFTER_LOGIN is a command to go to page i want (i did chnage it to try and did same) something to do with the on page code i think thats stoppin this (this beeing <?php if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { // not logged in, move to login page echo "your not allowed in" ; exit; } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 You have started a session on every page session_start(); should be at the top of the page Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 22, 2007 Author Share Posted November 22, 2007 dam... lol why didnt i add that, been looking at ocde you gave not getting why it would not work snd i didnt even start the session even looked at it in function checkLogin ( $levels ) { session_start ();< --- right infront of my eyes lol thanks for help!! if u ever need help i will return favour lol Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 22, 2007 Share Posted November 22, 2007 it ok. We as newbie also overlook the obvious. HAHA. i loaded up all my script to see what i had miss and i have comments all over say //Dont forget to start the session. Quote Link to comment Share on other sites More sharing options...
Azu Posted November 22, 2007 Share Posted November 22, 2007 <?php if ( $_SESSION['logged_in'] ){ ?> welcome member <?php endif; ?> here is the code to prevent users from viewing page. yes simple i know but this is not the problem for the life of me i cant do it lol what i want is an error message to show if the user is not logged in. i have tried adding <?php } else { ?> <p>This is the protected page. Your private content goes here.</p> <?php } ?> and verius other methords and know luck... i should now how to do this but its not there lol <=$_SESSION['logged_in']?'You are logged in.':'You are not logged in.';?> 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.