jamesxg1 Posted March 1, 2009 Share Posted March 1, 2009 How do i make a IF function that will display some content if the user is logged in ? heres my login script <?php $dbhost="localhost"; $dbusername="root"; $dbpassword=""; $database_name="simple"; $admin_tbl="admin"; mysql_connect("$dbhost", "$dbusername", "$dbpassword")or die("cannot connect"); mysql_select_db("$database_name")or die("cannot select DB"); $username=$_POST['username']; $password=$_POST['password']; $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM $admin_tbl WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ session_register("username"); session_register("password"); session_register("groupid"); header("location:../index.php"); } else { echo "Wrong Username or Password"; } ?> i need to put a IF function on the index.php page so that it will display content only for logged in user's Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/ Share on other sites More sharing options...
jamesxg1 Posted March 1, 2009 Author Share Posted March 1, 2009 Anyone have any idea's please ? Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773836 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 First of don't use functions such as session_is_registered or session_registered. These are depreciated and should only be used if register_globals is enabled, which you should NEVER enable as that too is depreciated and is to be removed completely as of PHP6. When setting or accessing session variables just use the $_SERVER superglobal as you would with $_GET or $_POST superglobals. So I'd change this session_register("username"); session_register("password"); session_register("groupid"); to $row = mysql_fetch_assoc($result); // get the results from the query $_SESSION['username'] = $row['username']; $_SESSION['groupid'] = $row['groupid']; Now to see if the user is logged in on your pages do this simple check <?php // always call session_start on any page that uses sessions session_start(); // if the username or groupid session variables are not set then user is not logged in! if(!isset($_SESSION['username'], $_SESSION['groupid'])) { // user is not logged, redirect the user or display a message echo 'Please login to view this page!'; exit; } // rest of your code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773849 Share on other sites More sharing options...
jamesxg1 Posted March 1, 2009 Author Share Posted March 1, 2009 First of don't use functions such as session_is_registered or session_registered. These are depreciated and should only be used if register_globals is enabled, which you should NEVER enable as that too is depreciated and is to be removed completely as of PHP6. When setting or accessing session variables just use the $_SERVER superglobal as you would with $_GET or $_POST superglobals. So I'd change this session_register("username"); session_register("password"); session_register("groupid"); to $row = mysql_fetch_assoc($result); // get the results from the query $_SESSION['username'] = $row['username']; $_SESSION['groupid'] = $row['groupid']; Now to see if the user is logged in on your pages do this simple check <?php // always call session_start on any page that uses sessions session_start(); // if the username or groupid session variables are not set then user is not logged in! if(!isset($_SESSION['username'], $_SESSION['groupid'])) { // user is not logged, redirect the user or display a message echo 'Please login to view this page!'; exit; } // rest of your code here ?> ok so how would i make it so that i can display just text on the page if they are logged in ? Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773861 Share on other sites More sharing options...
corbin Posted March 1, 2009 Share Posted March 1, 2009 An else clause? Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773865 Share on other sites More sharing options...
wildteen88 Posted March 1, 2009 Share Posted March 1, 2009 Did you see the code snippet at the bottom of my page. Thats all you need to have. You just add the code/text you want displayed when the user is logged after the line that says // rest of your code here. The code before that line prevents the user from accessing the page if they are not logged in. Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773866 Share on other sites More sharing options...
jamesxg1 Posted March 1, 2009 Author Share Posted March 1, 2009 Did you see the code snippet at the bottom of my page. Thats all you need to have. You just add the code/text you want displayed when the user is logged after the line that says // rest of your code here. The code before that line prevents the user from accessing the page if they are not logged in. Hiya, I need the guests and the users to be able to view the page but if the user is logged in it will display certain things that a guest cant, how would i go about this ? Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773878 Share on other sites More sharing options...
jamesxg1 Posted March 1, 2009 Author Share Posted March 1, 2009 ok is there a way i can do the following ? IF $_SESSION['username']; does exist display specific content ? Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773898 Share on other sites More sharing options...
corbin Posted March 1, 2009 Share Posted March 1, 2009 if(something) { //do something } else { //do something else } Quote Link to comment https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/#findComment-773905 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.