0

i have two scripts. One is named as index.php and other is home.php.

index.php is used for login (verification of username and password using in_array) and then redirecting user to home.php. Here is index.php:

<?php
session_start();
?>

<html>
<head>
    <title>Log In</title>
</head>


<body><center>

    <form method="POST" action="index.php" ">
        <h2>Log In</h2>
        Username: <input type="text" id="uname" name="username" placeholder="Your Username" maxlength="20"/><br/>
        Password: <input type="password" id="pwd" name="password" placeholder="Your Password" maxlength="20"/><br/><br/>
        <input type="hidden" name="form_status" value="sent"/>
        <input type="submit" value="Log In"/>
    </form></center>

<?php

    $userdetails = array('james'=>'james123','john'=>'john123','stefan'=>'stefan123');

    if(isset($_POST['form_status']) && $_POST['form_status']=="sent"){
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['password']= $_POST['password'];  


        if(isset($_SESSION['username'])){
            if(in_array($_SESSION['password'],$userdetails)){
                header("location: home.php");
            }
        }
        else {
            echo "Your password is incorrect";
        }


    }
?>
</body>
</html>

In the second script (home.php), i have stored qualification and interests of users(in a multidimensional array) and then using that array to display the interest and qualification of loggedin user in home.php. Here is the script:

<?php
    session_start();

    if(isset($_SESSION["username"])){

        $userdetails = array(
                        array("username"=>"james","qualification"=>"LLB from Harward.", "interests"=>"baseball, football and sports cars"),
                        array("username"=>"john","qualification"=>"Commerce.", "interests"=>"weight lifting, cars and cycling"),
                        array("username"=>"stefan","qualification"=>"MBBS.", "interests"=>"reading, photography and Gaming")
                        );              


            echo "Hello ". $_SESSION["username"];

                if($_SESSION['username']=="james"){
                    echo "<br/>Your qualification is ".$userdetails[0]['qualification'];
                    echo "<br/>Your interests are ".$userdetails[0]['interests'];
                    echo "<br/><a href='friends.php'>Your Friends</a><br/><a href='session_destroy.php'>Log Out</a><br/>";
                }

                if($_SESSION['username']=="john"){
                    echo "<br/>Your qualification is ".$userdetails[1]['qualification'];
                    echo "<br/>Your interests are ".$userdetails[1]['interests'];
                    echo "<br/><a href='friends.php'>Your Friends</a><br/><a href='session_destroy.php'>Log Out</a><br/>";
                }

                if($_SESSION['username']=="stefan"){
                    echo "<br/>Your qualification is ".$userdetails[2]['qualification'];
                    echo "<br/>Your interests are ".$userdetails[2]['interests'];
                    echo "<br/><a href='friends.php'>Your Friends</a><br/><a href='session_destroy.php'>Log Out</a><br/>";
                }



    }else{
        header("Location: index.php");
    }
?>

Now the problem is that i have to write code for displaying qualification and interests of each user separately by using if else if statements.

Isnt is possible to use a single (or few statements) to display qualification and interest of a particular user to that user only?

PS. i know that it is recommended to use database for this purpose. I'm just practicing arrays and if elseif statements.

2 Answers 2

1

Use a key-value array for the usernames:

$userdetails = array(
    "james" => array("username"=>"james","qualification"=>"LLB from Harward.", "interests"=>"baseball, football and sports cars"),
    "john" => array("username"=>"john","qualification"=>"Commerce.", "interests"=>"weight lifting, cars and cycling"),
    "stefan" => array("username"=>"stefan","qualification"=>"MBBS.", "interests"=>"reading, photography and Gaming")
);

Then you can just use the username as the key:

$details = $userdetails[$_SESSION['username']];
echo "<br/>Your qualification is ".$details['qualification'];
echo "<br/>Your interests are ".$details['interests'];
echo "<br/><a href='friends.php'>Your Friends</a><br/><a href='session_destroy.php'>Log Out</a><br/>";
Sign up to request clarification or add additional context in comments.

1 Comment

and you could add the password to this array as well. then us it in an include file.
0

You can use foreach for $userdetails

   foreach($userdetails  as $k=>$userdetail){

     if($_SESSION['username']==$k){
         echo "<br/>Your qualification is ".$userdetail['qualification'];
         echo "<br/>Your interests are ".$userdetail['interests'];
         echo "<br/><a href='friends.php'>Your Friends</a><br/><a href='session_destroy.php'>Log Out</a><br/>";
     }

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.