If, else if - php

Soldato
Joined
26 Nov 2005
Posts
3,839
Location
Doon the Bay (Newcastle)
Hi i'm slightly confused and wondered if someone could look at this code for me and sort it. Cheers.

if($count==1){
session_register("ADMIN");
session_register("ADMIN");
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success1.php'>";
}

else if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success.php'>";
//header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}



Basically i want to direct the admin account to somewhere else from normal login, basically everything now goes to the admin account so the second part isn't working although the else part is.

Thanks.
 
if/else does one *or* the other, not both. If the first condition evaluates false, attempt the second condition.

You'll want this..
PHP:
if($count==1){
  session_register("ADMIN");
  session_register("ADMIN");
  echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success1.php'>";

  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword");
  echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success.php'>";
  //header("location:login_success.php");
}
else {
  echo "Wrong Username or Password";
}
 
Hi mate, that almost worked, but now the admin entry doesn't go to admin, just normal log in.

Here's the whole code if that might help:

PHP:
<?php
session_start();
include("getinfo.php");


// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM Members 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 $myusername and $mypassword, table row must be 1 row

if($count==1){
  session_register("ADMIN");
  session_register("ADMIN");
  echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success1.php'>";

  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("myusername");
  session_register("mypassword");
  echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success.php'>";
  //header("location:login_success.php");
}
else {
  echo "Wrong Username or Password";
}
?>
 
If I understand you, you want to re-direct an admin user who logs in to a different page than the normal users get?

In that case, how do you differentiate between admin / non-admin users? Is it a property of their user record in the database, or their IP, or something else?

Also, in your example you're echoing 2 meta-refreshes to the same page which are re-directing to different scripts... you only need one.
 
I find having a separate login for the admin on a different page perhaps rather than trying to differentiate in one query.

So basically you have a separate table for the admin details.

Code:
$sql="SELECT * FROM Admin WHERE adminusername='$myusername' and adminpassword='$mypassword'";
 
From what I can see, you've no way to differentiate between a normal user and an admin?

PHP:
<?php
session_start();
include("getinfo.php");


// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$admin = "YourUserName";

if( $_POST['myusername'] == $admin )
{
 $sql="SELECT * FROM Members WHERE username='$myusername' and password='$mypassword'";
 $result=mysql_query($sql);
 $count=mysql_num_rows($result);
 if( $count == 1 ) { $isAdmin = true; $doLogin = true; } else { $isAdmin = false; $doLogin = false; }
} else {
 $sql="SELECT * FROM Members WHERE username='$myusername' and password='$mypassword'";
 $result=mysql_query($sql);
 $count=mysql_num_rows($result);
 if( $count == 1 ) { $isAdmin = false; $doLogin = true; } else { $isAdmin = false; $doLogin = false; }
}

if( $doLogin == true )
{
 if( $isAdmin == true )
 {
     session_register("ADMIN");
     session_register("ADMIN");
     echo "<META HTTP-EQUIV='Refresh' CONTENT='0; 
URL=login_success1.php'>";
 } else {
     session_register("myusername");
     session_register("mypassword");
     echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success.php'>";
 }
} else {
 echo "Something went wrong";
}
?>

Best way I can think of doing it without either having a seperate database for admin details, or a 'UserRights' column in the Members database (e.g. '1' for normal users, '2' for Admin, etc).

Bit messy mind.
 
Last edited:
Sorted it in the end: Was a silly mistake really:

PHP:
<?php
session_start();
include("getinfo.php");


// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM Members 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 $myusername and $mypassword, table row must be 1 row
echo "$count" . "$myusername" . " " . "$mypassword";
if($count==1){
	if ($myusername == 'ADMIN' && $mypassword == 'ADMIN') {
  		session_register("myusername");
  		session_register("mypassword");
  		echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success1.php'>";
	} else {
		//echo "has gone past the success1";
  		// Register $myusername, $mypassword and redirect to file "login_success.php"
  		session_register("myusername");
  		session_register("mypassword");
		echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=login_success.php'>";
  		header("location:login_success.php");
	}
} else {
  echo "Wrong Username or Password";
}
?>
 
Now i just need to amend values from a DB.

Can't even begin to think how i should be doing that. Ughhh...

Thanks for the help and advice, much appreciated.
 
Back
Top Bottom