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.