index.php用于登录(使用in_array验证用户名和密码),然后将用户重定向到home.php。这是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>
在第二个脚本(home.php)中,我已经存储了用户的资格和兴趣(在多维数组中),然后使用该数组显示home.php中登录用户的兴趣和资格。这是脚本:
<?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");
}
?>
现在的问题是我必须使用if else if语句分别编写代码来分别显示每个用户的资格和兴趣。
Isnt是否可以使用单个(或几个语句)来向该用户显示特定用户的资格和兴趣?
PS。我知道建议为此目的使用数据库。我只是练习数组和if ifif语句。
答案 0 :(得分:1)
为用户名使用键值数组:
$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")
);
然后你可以使用用户名作为密钥:
$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/>";
答案 1 :(得分:0)
您可以将foreach
用于$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/>";
}
}