Setting a cookie when $_POST is being supplied

I have this code:

[php]$files = file_get_contents(“passwords.txt”);
@ $username = $_POST[‘u’];
@ $password = $_POST[‘p’];
@ $checkbox = $_POST[‘remember’];
@ $submit = $_POST[‘submit’];

// strpos to setup that $username & $password matches the textfile
$pos = strpos($files, “$username,$password”.PHP_EOL);

// if the username and password match
if($pos !== false){
setcookie(‘uname’, $username);
setcookie(‘loggedintime’, time() );
drawForm(’’,’’);
$_SESSION[‘logintime’] = time();
$_SESSION[‘timeloggedin’] = time() - $_SESSION[‘logintime’];
echo “Welcome “.$_POST[‘u’].”

”;
echo “You have been logged in for “.$_SESSION[‘timeloggedin’].” seconds

”;
echo "
Browse books in store

Book analytics

Log out
";

exit();

}

if(($pos !== false) && (isset($checkbox))){
setcookie(‘loggedin’, $_COOKIE[‘uname’], time() + 60 * 20);
print_r($_COOKIE[‘uname’]);
drawForm($_COOKIE[‘uname’], ‘checked’);
}
[/php]

One thing that isn’t working when the checkbox is do checked, it’s still not recognizing it.
It’s supposed to set a cookie of the username if the POST is being supplied and the checkbox is checked that is set to expire after 20 min to index.php and would automatically fill in the username textbox and the cookie’s value and that the checkbox must be checked as well.

Please bare with me I’m a novice as in we just finished our 6th class and this assignment is part 1 of 4.

Could you add the html / form as well?

btw, supressing errors is considered bad practise. I would suggest changing the first lines to something like this:
[php] $files = file_get_contents(“passwords.txt”);
$username = !empty($_POST[‘p’]) ? $_POST[‘p’] : null;
$password = !empty($_POST[‘u’]) ? $_POST[‘u’] : null;
$checkbox = !empty($_POST[‘remember’]) ? $_POST[‘remember’] : null;
$submit = !empty($_POST[‘submit’]) ? $_POST[‘submit’] : null;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service