user2740749
user2740749

Reputation:

Variables Comparison PHP

I'm really new at this so please be patient. I try to do a very basic operation: Compare two strings. It seemed easy enough but I can't get it work. Here is a bit of code I made to check the value of $username before going further. But whatever value I give to $username it seems it never go through the if/else test since I never get any output on the screen. Why is that? Is it a syntax error, something else?

Thanks for help.

<?php
    $username = $_SESSION['Sess_User'];
    $password = $_SESSION['Sess_Code'];
    if (strcmp($username, "Admin") !== 0){
        echo "Acces Denied.";
    }else{ 
        echo "Ok, Go on.";
    }
?>

Upvotes: 0

Views: 52

Answers (4)

Jishad
Jishad

Reputation: 2965

Try This,

<?php
session_start();
$username = $_SESSION['Sess_User'];
$password = $_SESSION['Sess_Code'];
if ($username=="Admin"){
  echo "Ok, Go on.";

}else{ 
    echo "Acces Denied.";
}
?>

Upvotes: 0

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27242

Just use simple these things :

// For start a session

session_start();

// assign session value into variables

$username = $_SESSION['Sess_User'];
$password = $_SESSION['Sess_Code'];

// check comparision by using comparision operator

if ($username=="Admin"){
    echo "Acces Denied.";
}else{ 
    echo "Ok, Go on.";
}

Upvotes: 1

Anand Patel
Anand Patel

Reputation: 3943

use session_start(); at top of file

and strcmp is for case sensitive comparison you can use strcasecmp or directly comparison operator(==)

<?php
    session_start();
    $username = $_SESSION['Sess_User'];
    $password = $_SESSION['Sess_Code'];

    //you can also use if($username == "Admin")
    if (strcasecmp($username, "Admin") !== 0){
        echo "Acces Denied.";
    }else{ 
        echo "Ok, Go on.";
    }
?>

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

<?php
    session_start();
    $username = $_SESSION['Sess_User'];
    $password = $_SESSION['Sess_Code'];
    if (strcmp($username, "Admin") !== 0){
        echo "Acces Denied.";
    }else{ 
        echo "Ok, Go on.";
    }
?>

Upvotes: 0

Related Questions