Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/147433-how-do-i-make-a-if-function-for-this/
Share on other sites

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
?>

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 ?

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.

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 ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.