Wednesday, 11 September 2013

PHP Redirect Depending On User Type

PHP Redirect Depending On User Type

I have a login page that fairly basic. I'm developing a prototype whilst
learning the in's and outs of PHP.
In my MySql database, it looks like this: UserID, username, passcode,
talent, customer. Talent and customer are both binary and only one can be
True.
So what I'm trying to do, is if talent = 1, send them to index.php, and if
customer = 1, send them to recruiter.php.
I'm 90% sure I have the basics down, but since I've added the extra if's
to check talent and customer, it's broken my script :)
Finally, I'd need to do this check on each page to make sure that
customers don't access talent pages and visa versa.
Am I on the right track??
My current script does this:
<?php
include("resource/config.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);
$sql="SELECT UserID, talent, customer FROM useradmin WHERE
username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must
be 1 row
if (['talent']==1) {
if ($count==1) {
$_SESSION['login_user']=$myusername;
header("location: index.php");
}
else {
$error="Your Email or Password is invalid";
}
}
elseif (['customer']==1) {
if ($count==1) {
$_SESSION['login_user']=$myusername;
header("location: recruiter.php");
}
else {
$error="Your Email or Password is invalid";
}
}
else {
$error="Your Login Name or Password is invalid";
}
}
?>

No comments:

Post a Comment