【发布时间】:2016-11-22 11:34:41
【问题描述】:
首先,我将粘贴我的代码,然后会解释我的问题是什么。
Index.php
<?php
$userPassword=array(
'paul'=>'123456',
'chiri'=>'hola',
'maria'=>'adios'
);
$nameUser='';
$passwordUser='';
$errorMessage='';
function firstExecution(){
if(isset($_SERVER['HTTP_REFERER'])){
if(strpos($_SERVER['HTTP_REFERER'],$_SERVER['PHP_SELF'])!=FALSE){
return false;
}else{
return true;
}
}else{
return true;
}
}
if(firstExecution()==false){
$nameUser=$_GET['user'];
$passwordUser=$_GET['password'];
$errorMessage="Wrong data";
while($cred = current($userPassword)) {
if($cred == $passwordUser) {
if(key($userPassword) == $nameUser) {
header('Location: counter.php');
break;
} else {
$errorMessage='Wrong data';
break;
}
} else {
$errorMessage='Wrong data';
break;
}
}
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="errors" style="color: red;"><?php echo $errorMessage; ?></div>
Name: <input type="text" name="user" value=""/>
<br/>
Password: <input type="text" name="password" value=""/>
<br/>
<input type="submit" name="send" value="send"/>
</form>
</body>
</html>
Counter.php
<?php
error_reporting(0);
$cookieName=$_GET['user'];
$cookieValue=$_GET['Jobs'];
/*We create the array with Values for Radio Button group*/
$arrayJobs=array(
'Programmer'=>'Programmer',
'Lawyer'=>'Lawyer',
'Doctor'=>'Doctor'
);
/*Function to create Radio Buttons*/
function generateRadioButton($name, $valueArray, $selectedValue) {
$exit = '';
foreach ($valueArray as $key => $value) {
if ($selectedValue == $key) {
$exit .= '<label>' . $value . '</label><input type="radio" name="'. $name .'" value="' . $key . '" checked/>' . PHP_EOL;
} else {
$exit .= '<label>' . $value . '</label><input type="radio" name="'. $name .'" value="' . $key . '" />' . PHP_EOL;
}
}
return $exit;
}
/*We create the cookie if submit has been clicked and go to Index.php*/
if(isset($_GET['disconnect'])){
setcookie($cookieName, $cookieValue, time()+3*24*3600);
header('Location: index.php');
}
?>
<html>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo generateRadioButton('Jobs', $arrayJobs, $cookieValue);?>
<?php echo $cookieName?>
<br>
<input type="submit" name="disconnect" value="Disconnect"/>
</form>
</html>
在 index.php 上,我有一个登录表单,这些凭据存储在一个数组中(我们还没有使用 db,所以现在我们必须这样做),如果登录错误,它将显示一条错误消息,如果正确,它将转到 counter.php;在 contador.php 我有 3 个单选按钮和一个断开连接按钮,因此,当单击断开连接时,它必须使用所选单选按钮的值和用户名创建一个 cookie。
现在,我的问题是,cookie 已创建但没有任何名称,由于某种原因我找不到,counter.php 没有得到 index.php 上引入的名称;我可以想象它与 header() 的使用有关,但我不知道如何保持对登录的限制,同时将登录的值发送到 counter.php
【问题讨论】:
-
研究什么是会话(仅安全),或在
index.php上,重定向到counter.php,并使用包含用户名的附加查询字符串(根本不安全),以便counter.php知道用户。 -
这个故事中的 contador.php 在哪里?
标签: php