PHP if and else statements

Hi people i have this code below, everything works fine but i want to add an else statement to all these. I want it to use an else statement if none of these IF statements show up, how can i do that? I have tried but it just mucks up all the statements.:


<?php
include "conf/db_connect.php";
$ip = $_SERVER['REMOTE_ADDR']; 
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$level = registered;
$result=mysql_query("select * from member where username='$username' AND password='$password'"); 

$query = mysql_query("SELECT * FROM options ")
or die("Could not insert data because ".mysql_error());
$qry = mysql_fetch_array( $query );

$query2 = mysql_query("SELECT * FROM onlineusers where UserName='$username' ")
or die("Could not insert data because ".mysql_error());
$qry2 = mysql_fetch_array( $query2 );

$query3 = mysql_query("SELECT * FROM kickedip where Kickedip='$ip' ")
or die("Could not insert data because ".mysql_error());
$qry3 = mysql_fetch_array( $query3 );

if ( $qry[disabled] == yes ) {
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=9">
<?php
}

if ($qry[chatoffline] == 'yes') {
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=8">
<?php
}

$handle = fsockopen("$chatservername", $chatport, $err_no, $err_str, 2);
if(false === is_resource($handle))
{
?>
<body onload="makeAlert('System Message','Chat server is offline, please try again later!');">
<?php
}

if (mysql_num_rows($qry3) == 1)
{
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=7">
<?php
}

//check that at least one row was returned 

$rowCheck = mysql_num_rows($result); 

if (mysql_num_rows($qry2) == 1)
{
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=5">
<?php
}

if (! eregi("^[a-zA-Z0-9 ]*$", $_POST["username"]) ) {
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=3">
<?php
}

if ($qry2[MemberType] == '5') {
?>
<meta http-equiv="REFRESH" content="0;url=login.php?error=6">
<?php
}

    ?> 

Here is one way to do


var $actionTaken = FALSE;
if (...) {
    // do something and then
    $actionTaken = TRUE;
}
if (...) {
    // do something and then
    $actionTaken = TRUE;
}
if (!$actionTaken) {
    ...
}

But really, a refactoring of the original post should be done so that it’s easier to achieve what you’re after.

ok i have re-done all the code, at the moment everything is working except for the bottom part where if i type the login details correct or wrong it still redirects to the link with error4 in the url. I have the php errors turned on and none show. My code is below:


<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);

include "conf/db_connect.php";
$ip = $_SERVER['REMOTE_ADDR']; 
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$level = 'registered';

$query = mysql_query("SELECT * FROM options ")
or die("Could not insert data because ".mysql_error());
$qry = mysql_fetch_array( $query );

$query2 = mysql_query("SELECT * FROM onlineusers WHERE UserName='$username' ")
or die("Could not insert data because ".mysql_error());
$qry2 = mysql_fetch_array( $query2 );

$query3 = mysql_query("SELECT * FROM kickedip WHERE Kickedip='$ip' ")
or die("Could not insert data because ".mysql_error());
$qry3 = mysql_fetch_array( $query3 );

$query4 = mysql_query("SELECT * FROM member WHERE UserName='$username' AND Password='$password'")
or die("Could not insert data because ".mysql_error());
$qry4 = mysql_fetch_array( $query4 );


//CHECK IF CHAT IS DISABLED
	if( $qry['disabled'] == 'yes' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=9">';
	}

//CHECK IF CHAT IS OFFLINE IN THE MYSQL DATABASE SETTING
elseif( $qry['chatoffline'] == 'yes' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=8">';
	}

//CHECK IF CHAT SERVER IS OFFLINE COMING SOON
$handle = fsockopen("$chatservername", $chatport, $err_no, $err_str, 2);
if(!$handle)
{
echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=5">';
} 


//CHECK IF USER ACCOUNT IS DISABLED
elseif( $qry4['MemberType'] == '5' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=6">';
	}


//CHECK IF USER ACCOUNT IS VALIDATED
elseif( $qry4['Activated'] == 'no' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=10">';
	}

//CHECK IF USER IP IS IN RECENT KICK LIST
elseif( $qry3 == '1' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=7">';
	}

//CHECK IS IF YOU ARE ALREADY ONLINE
elseif( $qry2 == 'no' ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=5">';
	}

//CHECK FOR ILLEGIAL CHARACTERS
elseif (! eregi("^[a-zA-Z0-9 ]*$", $_POST["username"]) ) {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=3">';
	}

//IF ALL STATEMENTS ARE FALSE PROCEED WITH LOGIN
else {

if ($qry4 == '1')
{
session_start();
session_register('user');
$_SESSION['username'] = $username;
$_SESSION['level'] = $level;
header('Location: windows.php');
}

//IF LOGIN INFORMATION IS WRONG SHOW STATEMENT BELOW
else
{
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=4">';
}
}
?>


//...
$query4 = mysql_query("SELECT * FROM member WHERE UserName='$username' AND Password='$password'")
	or die("Could not insert data because ".mysql_error());
$qry4 = mysql_fetch_array( $query4 );
//...
else {
	if ($qry4 == '1') {
		//...
	}

	//IF LOGIN INFORMATION IS WRONG SHOW STATEMENT BELOW
	else {
		echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=4">';

	}
}

$qry4 will either be an array or false, neither will == ‘1’, so the else will always execute (as long as you got through the other elseifs).

He can get the right sort of check by performing:


if (!empty($qry4)) {

Although, it would help if the code were structured better. Here’s a reworking of it from off the top-of-my-head that should work a lot easier, and be easier to understand as well. The code is untested so be forewarned that might be a mis-key in there.


<?php 
ini_set('display_errors',1); 
error_reporting(E_ALL); 

include "conf/db_connect.php"; 

function validate_chat($chatServerInfo)
{
    $valid = TRUE;
    
    $query = mysql_query("SELECT * FROM options ") 
    or die("Could not select data because ".mysql_error()); 
    $result = mysql_fetch_array( $query ); 

    //CHECK IF CHAT IS DISABLED 
    if( $result['disabled'] == 'yes' ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=9">'; 
        $valid = FALSE;
    } 

    //CHECK IF CHAT IS OFFLINE IN THE MYSQL DATABASE SETTING 
    if( $result['chatoffline'] == 'yes' ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=8">'; 
        $valid = FALSE;
    } 

    //CHECK IF CHAT SERVER IS OFFLINE COMING SOON 
    $chatport = $chatServerInfo['chatport'];
    $err_no = $chatServerInfo['err_no'];
    $err_str = $chatServerInfo['err_str'];
    $handle = fsockopen("$chatservername", $chatport, $err_no, $err_str, 2); 
    if(!$handle) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=5">'; 
        $valid = FALSE;
    } 
    return $valid;
}

function validate_account($username, $password)
{
    $valid = TRUE;
    $query = mysql_query("SELECT * FROM member WHERE UserName='$username' AND Password='$password'") 
    or die("Could not select data because ".mysql_error()); 
    $result = mysql_fetch_array( $query ); 

    //CHECK IF USER ACCOUNT IS DISABLED 
    if ( $result['MemberType'] == '5' ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=6">'; 
        $valid = FALSE;
    } 

    //CHECK IF USER ACCOUNT IS VALIDATED 
    if ( $result['Activated'] == 'no' ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=10">'; 
        $valid = FALSE;
    } 
    return $valid;
}

function in_kick_list($ip)
{
    $valid = TRUE;
    $query = mysql_query("SELECT * FROM kickedip WHERE Kickedip='$ip' ") 
    or die("Could not insert data because ".mysql_error()); 
    $result = mysql_fetch_array( $query ); 

    //CHECK IF USER IP IS IN RECENT KICK LIST 
    if( !empty($result) ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=7">'; 
        $valid = FALSE;
    } 
    return $valid;
}

function already_online($username)
{
    $valid = TRUE;
    $query = mysql_query("SELECT * FROM onlineusers WHERE UserName='$username' ") 
    or die("Could not insert data because ".mysql_error()); 
    $result = mysql_fetch_array( $query ); 

    //CHECK IS IF YOU ARE ALREADY ONLINE 
    if( !empty($result) ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=5">'; 
        $valid = FALSE;
    } 
    return $valid;
}

$chatServerInfo = array(
    'chatport' => $chatport,
    'error_no' => $err_no,
    'err_str' => $err_str
);

function bad_chars($username)
{
    $valid = TRUE;
    //CHECK FOR ILLEGIAL CHARACTERS 
    if (! eregi("^[a-zA-Z0-9 ]*$", $username) ) { 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=3">'; 
        $valid = FALSE;
    }
    return $valid;
}

$ip = $_SERVER['REMOTE_ADDR']; 
$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 
$level = 'registered'; 

$chatAllowed = validate_chat($chatServerInfo);
$accountAllowed = !in_kick_list($ip) && already_online($username);
$noBadChars = !bad_chars($_POST["username"]);

//IF ALL STATEMENTS PASS PROCEED WITH LOGIN 
if ($chatAllowed && $accountAllowed && $noBadChars) {
    $validAccount = validate_account($username, $password);
    if ($validAccount) { 
        session_start(); 
        session_register('user'); 
        $_SESSION['username'] = $username; 
        $_SESSION['level'] = $level; 
        header('Location: windows.php'); 
    } else { 
        //IF LOGIN INFORMATION IS WRONG SHOW STATEMENT BELOW 
        echo '<meta http-equiv="REFRESH" content="0;url=login.php?error=4">'; 
    } 
} 
?>