Jump to content

Redirect admin to different page


ckerr27

Recommended Posts

hello, I have created a website which has to incorporate administration features. I hjave been trying to do this for a couple of days and any help will be much appreciated.

I have a login screen (form) when the user enters their username and password they are brought to the homepage.php. Is there anyway to redirect the administrator (username=admin) to a different page from all ordinary members? e.g. deletemember.php

 

Thanks

 

Link to comment
Share on other sites

you need to look for a php log in system there really not that difficult but they deal with some pretty useful php features.

 

What you would want to do is set a session variable when they log in and are an admin.  Then forward them to the page you want with a header.

 

You would still want to keep a check on that page for that session variable though as someone could just type in that page and be able to access admin features.

 

This is the tutorial i followed a while back.

 

http://www.phpeasystep.com/phptu/6.html

Link to comment
Share on other sites

I have created the login feature already, It works fine, I am just confused as how to add the admin feature, how do i direct only the admin to the deletemember.php page.

The code below shows my login code it is very similar to the tutorial you used.

 

Form code:

 

<form name="form1" method="post" action="checklogin.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td colspan="3"><strong>Member Login </strong></td>

</tr>

<tr>

<td width="78">Username</td>

<td width="6">:</td>

<td width="294"><input name="myusername" type="text" id="myusername"></td>

</tr>

<tr>

<td>Password</td>

<td>:</td>

<td><input name="mypassword" type="text" id="mypassword"></td>

</tr>

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Login"></td>

</tr>

</table>

</td>

</form>

</tr>

</table>

<br><br><center>Enter your usename and password....</center>

 

 

Checklogin.php

 

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// username and password sent from form

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

// To protect MySQL injection (more detail about MySQL injection)

$myusername = stripslashes($myusername);

$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);

$mypassword = mysql_real_escape_string($mypassword);

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $username and $password, table row must be 1 row

 

if($count==1){

// Register $username, $password and redirect to file "home.html"

session_register("myusername");

session_register("mypassword");

header("location:home.php?myusername=" . $username);

}

else {

echo "Wrong Username or Password";

}

?>

 

 

Link to comment
Share on other sites

In the code here change the header to what page you want them to see.

 

if($count==1){
// Register $username, $password and redirect to file "home.html"
session_register("myusername");
session_register("mypassword");
header("location:home.php?myusername=" . $username);
}

 

That code though only checks if they are registered... You will need to create something that checks if they have admin rights from the user table in your database.

 

YOu should have something there that denotes who is an admin and who is not.  Then set a session variable to only those users that is unique and will never accidentaly be given to a non admin user.

 

Once you have that session set.  You can do a session check on any page.

 

if(!(isset($_SESSION['isadmin']))) {
    header("location:error404.php"):  //this can be any page you want a non admin to see if they attempt to access a page designed only for admin use.
} else {
    here you can display whatever you want the admin to see.  I can be tables that access databases or whatever you like.

then close it at the bottom of you page }

 

Sessions can get a little tricky though so have a look at this.

 

http://php.net/manual/en/features.sessions.php

 

Also please check it out as there is probably a better way to have this done.  That is just a quick way.

Link to comment
Share on other sites

I don't know how many times I've seen this log in code but wherever it's from, stop using it.  session_register is depreciated. Make you page as follows.

<?php
session_start();
// any  pre-processing code goes here and DB connection

//Grab useful information with query like userid, level
$sql="SELECT userid,level FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
if (mysql_num_rows($result)){
$row = mysql_fetch_row($result);
//Set useful information to session, but NEVER password
$_SESSION['myusername']=$myusername;
$_SESSION['userid']=$row[0];
$_SESSION['level']=$row[1];

if ($_SESSION['level']=="admin"){
header("location: adminhome.php");
}else{
header("location: memberhome.php");
}
}else{
//set error to variable to display below
$error="Wrong Username or Password";
}
// html and head goes here
?>
<html>
<body>
<?php if (isset($error)) { echo $error; } ?>
form goes here

</body>
</html>

You should add code to check $_SESSION['level'] on all protected pages.

Link to comment
Share on other sites

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.