Jump to content

[SOLVED] simple, checkbox/cookie question


Lodius2000

Recommended Posts

tis really my first time working with checkboxes and cookies.

 

on my login script, clicking the checkbox 'remember me' will set a cookie

<input type="checkbox" name="remember" value="yes" />

 

later on down the line this is the php

<?php
if($_POST['remember'] == 'yes'){
setcookie('username', $username, (time() + (60*60*24*14)));
}
?>

 

something is messed up somewhere, because the cookie is getting set whether the checkbox is checked or not

 

any help would be much obliged

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/145701-solved-simple-checkboxcookie-question/
Share on other sites

angelcool,

I follow your logic perfectly, it makes sense to me too, but it didnt work.

 

I reset everything back to how it was in my original post

 

I also made a quick test form and ran print_r on $_POST

it gave these results:

 

when not checked

Array ( [submit] => submit [_submit_check] => 1 )

when checked

Array ( [remember] => yes [submit] => submit [_submit_check] => 1 )

 

dont mind _submit_check, it just blocks the double submit problem

 

but from what I see here my if() statement from above should work... but it doesnt

 

what else could it be

I set a variable

 

$_SESSION['username'] = $username;

 

directly above the if() that sets the cookie... since sessions are stored in cookies could it be that it is spoofing my system and $username is infact set in a cookie, though it isnt the cookie that I mean it to be, because at the top of this login page i have

 

if(isset($_COOKIE['username'])){

header('Location: home.php');

} else {

//go on with page

 

does that make sense, am I rambling, am i spoofing my own system?

I just ran:

<?php
if($_POST['remember'] == true)
echo 'true';
else 
  echo 'false';
?>
<html>
<head></head
<body>
<form action='' method="post">
<input type="checkbox" name="remember"/>
<input type="submit" />
</form>
</body>

 

and it works as it suppose to.

 

Could you post the complete code for your form and PHP ?

 

sure thing... its long

 

heres the php

<?php


session_start();


require ('../../PEAR/PEAR/DB.php');
require ('../../storeoutofaccess/db_login.php');
require ('../../storeoutofaccess/settings.php');
require ('requires/formhelpers.php');
$db->setErrorHandling(PEAR_ERROR_DIE);
$db->setFetchMode(DB_FETCHMODE_ASSOC);


// main page logic
if(isset($_COOKIE['username'])){
header('Location: home.php');
} else {
if($_POST['_submit_check']){
	if($form_errors = validate_form()){
		show_form($form_errors);
	} else {
		process_form();
	}
} else {
	show_form();
}
}

function show_form($errors = '') {
/* header.html makes top of page */
require('requires/header.html');	

/* display form */
require('requires/login.html');

/* footer.html closes all open tags */
require('requires/footer.html');
}

function validate_form(){
global $db, $salt;
$username = ($_POST['username']);

//check that username is entered
if (trim(strlen($username)) == 0) {
	$errors[]= "You must enter a username.";
}

//check that username is only letters or numbers
if (! preg_match('/^[a-zA-Z0-9]+$/i', $username)){
	$errors[]= "Your username must be <i><b>ONLY</b></i> letters or numbers.";
}


//check that password is entered
if (trim(strlen($_POST['password'])) == 0) {
	$errors[]= "You must enter a password.";
}

//determine whether user is valid and is changing temp_password or password
$password = $_POST['password'];
$hash = hash("sha512",$password.$salt);
$a = $db->query("SELECT id FROM users WHERE username = ? AND password = ? AND active = 1", array($username, $hash));
if ($a->numrows() == 0 ){
	$b = $db->query("SELECT id FROM users WHERE username = ? AND temp_password = ? AND active = 1 AND temp_usage = 0", array($username, $hash));
	if ($b->numrows() == 0 ){
		$errors[] = "Please enter a valid username and password";
	}
}


return $errors;

}

function process_form(){
global $db, $salt;
$username = trim($_POST['username']);
$password = $_POST['password'];
$hash = hash("sha512",$password.$salt);

$a = $db->getOne("SELECT password FROM users WHERE username = ?", array($username));
if ($a == $hash ){
	$_SESSION['username'] = $username;

	if($_POST['remember'] == 'yes'){
		setcookie('username', $username, (time() + (60*60*24*14)));
	}

	header('Location: home.php');

} else {
	$db->query("UPDATE users SET temp_usage = 1 WHERE username = ? and temp_password = ?", array($username, $hash));
	$_SESSION['username'] = $username;
	$_SESSION['message'] = 'Change your password now, you will not be able to log in with the temporary password again, after changing the password, you will be prompted to log in again';
	header('Location: changepw.php');
}

}

?>

 

heres the form... included as login.html

<form name="login" method="POST" action="<?php htmlentities($_SERVER['PHP_SELF']); ?>">
<table>

<tr>
<td>Username:</td>
<td><?php input_text('username', $_POST); ?></td>
</tr>

<tr>
<td>Password:</td>
<td><?php input_password('password', $_POST); ?></td>
</tr>

<tr>
<td>Remember me:</td>
<td><input type="checkbox" name="remember" value="yes" /></td>
</tr>

<tr>
<td></td>
<td><?php input_submit('submit', 'Log in'); ?></td>
</tr>

<input type="hidden" name="_submit_check" value="1" />

</table>
</form>

 

heres the part of formhelpers.php you need checkbox code is at the bottom

<?php

//print a text box
function input_text($element_name, $values, $size='20'){
print '<input type="text" name="' . $element_name .'" value="';
print htmlentities($values[$element_name]) . '"';
if(isset($size)){
	print ' size="'.$size.'"';
}
print ' />';
}

//print a password box
function input_password($field_name, $values, $size='20') {
print '<input type="password" name="' . $field_name .'" value="';
print htmlentities($values[$field_name]) . '"';
if(isset($size)){
	print 'size="'.$size.'"';
}
print ' />';
}

//print a submit button
function input_submit($element_name, $label){
print '<input type="submit" name="' . $element_name .'" value="';
print htmlentities($label) . '"/>';
}

/****************************checkbox code**********/
//print a radio button or checkbox
function input_radiocheck($type, $element_name, $values, $element_value){
print '<input type="' . $type . '" name="' . $element_name . '" value="' . $element_value . '" ';
if ($element_value == $values[$element_name]){
	print ' checked="checked"';
}
print '/>';
}

?>

ok this is really weird, I dont know what I did, but suddenly its working.

 

ive been working on it while waiting for a response, so I dont know whats different without manually comparing the code i posted above and what I have, but now its working

 

angelcool

 

thanks for your patience and help

 

now... bed is calling my name

 

SOLVED

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.