Jump to content

making new session variables


ja660k

Recommended Posts

hey guys, im having a problem making session variables. i have a login page and in it it has 2 "classes" of users, normal and super, and i do a check in my code and it assigns session variable accordingly, but when i print_r($_SESSION) the variable didnt work.

 

here is my code

function login($username,$password){
        $xml = simplexml_load_file("data/xml/users.xml") or die ("Fatal Error opening file");
        $auth = $xml->xpath("user/username[text()='$username']/../password[text()='$password']/..");
        // navigate to username and check if is same as entered username; do same for password.


        if($auth[0] !=null){
        // if auth found value
            if($xml->user->type == super){
            // check if user is super user (faculty)
                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password;
                $_SESSION['validAdminUser'] = true;
            }else if($xml->user->type == normal){
            // if not check if normal user
                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password;
                $_SESSION['validUser'] = true;
            }
        }else{
        // if auth did not find values
        $error = "Invalid Username or password";
        }
        return $error;
    }

 

and the xml for this looks like this

<users>
    <user>
        <type>normal</type>
        <username>ja660k</username>
        <password>admin</password>

        <firstName>zzzz</firstName>
        <lastName>qqqqq</lastName>
    </user>
    <user>
        <type>super</type>
        <username>admin</username>
        <password>admin</password>

        <firstName>xxxx</firstName>
        <lastName>yyyy</lastName>
    </user>
</users>

 

and when i try to login as admin

the print_r returns

Array ( [submit] => [username] => admin [password] => admin [validUser] => 1 ) 

instead of validuser, i need it to be validAdminUser like in the above php..?

can anyone help

Link to comment
https://forums.phpfreaks.com/topic/135741-making-new-session-variables/
Share on other sites

thankyou for making me think about it a little more, i solved the problem and the solution is this

 

function login($username,$password){
    $xml = simplexml_load_file("data/xml/users.xml") or die ("Fatal Error opening file");      
    $super = $xml->xpath("user/username[text()='$username']/../password[text()='$password']/../type[text()='super']");
    $normal = $xml->xpath("user/username[text()='$username']/../password[text()='$password']/../type[text()='normal']");

    if($super[0] !=null || $normal[0] !=null){
        if($super[0] != null){
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['validAdminUser'] = true;
        }else if($normal[0] != null){
            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;
            $_SESSION['validUser'] = true;
        }
    }else{
        $error = "Invalid Username or Password";
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.