Jump to content

how do i make a IF function for this ?


jamesxg1

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 ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.