Reputation: 334
firstly, I'm pasting my code and will explain after it what my issue is.
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>
On index.php I have a login form, and those credentials are stored in an array (we haven't worked with db yet so this is how we have to do this exercise now), if login is wrong it will show an error message, if correct it will go to counter.php; on contador.php I have 3 radio button and a disconnect button, therefore, when clicking on Disconnect it has to create a cookie with the value of the selected radio button and name of the user.
Now, my problem is, the cookie is created but without any name, for some reason I can't find, counter.php does not get the name introduced on index.php; I can imagine it's somehow related to the use of header() but I don't how to keep my restrictions on the login and at the same time send the values of the login to counter.php
Upvotes: 1
Views: 73
Reputation: 802
counter.php doesn't get the form data because the form data has been sent to index.php and you redirect to counter.php with the header method. The easiest thing would be to store the submitted data in the session variable.
See http://php.net/manual/en/reserved.variables.session.php
So in index.php right before the header() command, you type:
$_SESSION['user'] = $_GET['user'];and in counter.php you type:
$user = $_SESSION['user'];
Upvotes: 1