Remember me checkbox


#1

Hello!

Can someone please help me? My Remember me checkbox dosen’t work and since I added it everything got weird…
Everything worked fine before I added the remember me checkbox and php code for setting cookies. But now I write the email address and password to log in and I come to the user profile page but the menu dosen’t update and as soon as I go to a page that require that I’m logged in I got redirected to index.php (That happen if you ain’t logged in).
Can you guys check the code and see if you can find the problem?

Here’s the code that worked (without checkbox):

[php]

<?php include_once("php_includes/check_login_status.php"); // If user is already logged in, header that weenis away if($user_ok == true){ header("location: user.php?u=".$_SESSION["username"]); exit(); } ?><?php

// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST[“e”])){
// CONNECT TO THE DATABASE
require_once(“php_includes/db_conx.php”);
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db_conx, $_POST[‘e’]);

$p = hash("SHA512", $_POST['p']);

// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
	echo "login_failed";
    exit();
} else {
// END FORM DATA ERROR HANDLING
	$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
	$db_id = $row[0];
	$db_username = $row[1];
    $db_pass_str = $row[2];
	if($p != $db_pass_str){
		echo "login_failed";
        exit();
	} else {
		// CREATE THEIR SESSIONS AND COOKIES
		$_SESSION['userid'] = $db_id;
		$_SESSION['username'] = $db_username;
		$_SESSION['password'] = $db_pass_str;
		setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
		setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
		setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE); 
		// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
		$sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
		echo $db_username;
	    exit();
	}
}
exit();

}
?>

Log In #loginform{ margin-top:24px; } #loginform > div { margin-top: 12px; } #loginform > input { width: 200px; padding: 3px; background: #F3F9DD; } #loginbtn { font-size:15px; padding: 10px; } <?php include_once("template_pageTop.php"); ?>

Log In Here

Email Address:
Password:


Log In Forgot Your Password?
<?php include_once("template_pageBottom.php"); ?> [/php]

And here’s the new code (That won’t work):

[php]

<?php require_once("php_includes/check_login_status.php"); // If user is already logged in, header that weenis away if($user_ok == true){ header("location: user.php?u=".$_SESSION["username"]); exit(); } ?><?php

// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST[“e”])){
// CONNECT TO THE DATABASE
require_once(“php_includes/db_conx.php”);
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db_conx, $_POST[‘e’]);

$p = crypt("SHA512", $_POST['p']);

// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
	echo "login_failed";
    exit();
} else {
// END FORM DATA ERROR HANDLING
	$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
	$db_id = $row[0];
	$db_username = $row[1];
    $db_pass_str = $row[2];
	if($p != $db_pass_str){
		echo "login_failed";
        exit();
	} else if(isset($_POST['rememberme'])){
		// CREATE THEIR SESSIONS AND COOKIES
		setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
		setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
		setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);
		$_SESSION['userid'] = $db_id;
		$_SESSION['username'] = $db_username;
		$_SESSION['password'] = $db_pass_str;
	} else {
		$_SESSION['userid'] = $db_id;
		$_SESSION['username'] = $db_username;
		$_SESSION['password'] = $db_pass_str;
		// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
		$sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
		echo $db_username;
	    exit();
	}
}
exit();

}
?>

Log In #loginform{ margin-top:24px; } #loginform > div { margin-top: 12px; } #loginform > input { width: 200px; padding: 3px; background: #F3F9DD; } #loginbtn { font-size:15px; padding: 10px; } <?php include_once("template_pageTop.php"); ?>

Log In Here

Email Address:
Password:

 Remember me

Log In Forgot Your Password?
<?php include_once("template_pageBottom.php"); ?> [/php]

Thanks in advance,
Busarna4


#2

Add this right after your <?php tag

ini_set(‘display_errors’,1);
ini_set(‘display_startup_errors’,1);
error_reporting(-1);


#3

Hello!

I did that but nothing’s changed and no errors showed up…

Do you know what the problem could be? Just tell me if you need anything

Thanks in advance,
Busarna


#4

Just a guess, I didn’t try it… :slight_smile:

Underneath

[php]var p = _(“password”).value;[/php]

add

[php]var rm = _(“rememberme”).value;[/php]

Change

[php] } else if(isset($_POST[‘rememberme’])){[/php]

To:

[php] } else if(isset($_POST[‘rm’])){[/php]


#5

I think it works now :slight_smile: I ain’t 100% sure yet but it looks like it’s working.

THANKS


#6

Hello!

Can you guys help me another problem?

My email code dosen’t work…

Here’s the code:

[php]

<?php include_once("php_includes/check_login_status.php"); // If user is logged in, header them away if(isset($_SESSION["username"])){ header("location: message.php?msg=logged_in"); exit(); } ?><?php

// Ajax calls this NAME CHECK code to execute
if(isset($_POST[“usernamecheck”])){
include_once(“php_includes/db_conx.php”);
$username = preg_replace(’#[^a-z0-9]#i’, ‘’, $_POST[‘usernamecheck’]);
$sql = “SELECT id FROM users WHERE username=’$username’ LIMIT 1”;
$query = mysqli_query($db_conx, $sql);
$uname_check = mysqli_num_rows($query);
if (strlen($username) < 3 || strlen($username) > 100) {
echo ‘3 - 100 characters please’;
exit();
}
if (is_numeric($username[0])) {
echo ‘Usernames must begin with a letter’;
exit();
}
if ($uname_check < 1) {
echo ‘’ . $username . ’ is OK’;
exit();
} else {
echo ‘’ . $username . ’ is taken’;
exit();
}
}
?><?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST[“u”])){
// CONNECT TO THE DATABASE
include_once(“php_includes/db_conx.php”);
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$u = preg_replace(’#[^a-z0-9]#i’, ‘’, $_POST[‘u’]);
$e = mysqli_real_escape_string($db_conx, $_POST[‘e’]);
$p = $_POST[‘p’];
$g = preg_replace(’#[^a-z]#’, ‘’, $_POST[‘g’]);
$c = preg_replace(’#[^a-z ]#i’, ‘’, $_POST[‘c’]);
// GET USER IP ADDRESS
$ip = preg_replace(’#[^0-9.]#’, ‘’, getenv(‘REMOTE_ADDR’));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = “SELECT id FROM users WHERE username=’$u’ LIMIT 1”;
$query = mysqli_query($db_conx, $sql);
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = “SELECT id FROM users WHERE email=’$e’ LIMIT 1”;
$query = mysqli_query($db_conx, $sql);
$e_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($u == “” || $e == “” || $p == “”){
echo “The form submission is missing values.”;
exit();
} else if ($u_check > 0){
echo “The username you entered is alreay taken”;
exit();
} else if ($e_check > 0){
echo “That email address is already in use in the system”;
exit();
} else if (strlen($u) < 3 || strlen($u) > 100) {
echo “Username must be between 3 and 100 characters”;
exit();
} else if (is_numeric($u[0])) {
echo ‘Username cannot begin with a number’;
exit();
} else {
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Hash the password and apply your own mysterious unique salt
$p_hash = crypt(“SHA512”, $p);
// Add user info into the database table for the main site table
$sql = “INSERT INTO users (username, email, password, gender, country, ip, signup, lastlogin, notescheck)
VALUES(’$u’,’$e’,’$p_hash’,’$g’,’$c’,’$ip’,now(),now(),now())”;
$query = mysqli_query($db_conx, $sql);
$uid = mysqli_insert_id($db_conx);
// Establish their row in the useroptions table
$sql = “INSERT INTO useroptions (id, username, background) VALUES (’$uid’,’$u’,‘original’)”;
$query = mysqli_query($db_conx, $sql);
// Create directory(folder) to hold each user’s files(pics, MP3s, etc.)
if (!file_exists(“user/$u”)) {
mkdir(“user/$u”, 0755);
}
// Email the user their activation link
$to = “$e”;
$from = "no-reply@busarna4.pusku.com";
$subject = ‘busarna4.pusku.com Account Activation’;
$message = ‘Busarna4.pusku.com Message

busarna4.pusku.comBusarna4.pusku.com Account Activation
Hello ‘.$u.’,

Click the link below to activate your account when ready:

Click here to activate your account now

Login after successful activation using your:
* E-mail Address: ‘.$e.’
’;
$headers = “From: $from\n”;
$headers .= “MIME-Version: 1.0\n”;
$headers .= “Content-type: text/html; charset=UTF-8\n”;
mail($to, $subject, $message, $headers);
header(“Location: message.php?msg=signup_success”);
}
exit();
}
?> Sign Up #signupform{ margin-top:24px; } #signupform > div { margin-top: 12px; } #signupform > input,select { width: 200px; padding: 3px; background: #F3F9DD; } #signupbtn { font-size:18px; padding: 12px; } #terms { border:#CCC 1px solid; background: #F5F5F5; padding: 12px; } <?php include_once("template_pageTop.php"); ?>

Sign Up Here

Username*:
Email Address*:
Create Password*:
Confirm Password*:
Gender:
Male Female
Country:
<?php include_once("template_country_list.php"); ?>
View the Terms Of Use

Busarna4 Terms Of Use

1. Play nice here.

2. Don't swear.

3. Please contact us if you have any problems.



Create Account
<?php include_once("template_pageBottom.php"); ?> [/php]

The email code is on line 86-95.

Then I did this code:

[php]

<?php $to = "tma.s@hotmail.com"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; if (mail($to, $subject, $body)) { echo("

Email successfully sent!

"); } else { echo("

Email delivery failed…

"); } ?>

[/php]

And that worked. This code came to the junkbox but the first code didn’t show up in any inbox… But since the 2:nd code worked it shouldn’t be the server that ain’t set up right? Am I right? The server uses PHP 5.3 right now but I can update to PHP 5.4 if needed (I don’t think I can get 5.5). Can you take a look and see if you can find the problem? :slight_smile:

Thanks in advance,
Busarna


#7

I’m not sure…

Try to narrow down the issue…

Make sure $e has a valid email address and then try leaving off the headers. It can be something as simple as replacing all the \n with \r\n

You just have to play with it, I don’t think it’s a server problem, but most likely how it’s configured.


#8

It didn’t work if I replaced \n with \r\n…

Do you have any ideas of what I could do?

Thanks in advance,
Busarna


#9

Did you try all three things I posted?


#10

Yes I tried everything you told me to try but I can’t get the email…

Do you know something else?

Thanks in advance,
Busarna


#11

Do what you did in your separate code and see if it works in your main program…

Replace:

[php]mail($to, $subject, $message, $headers);[/php]

With:
[php] $to = "tma.s@hotmail.com";
$subject = “Hi!”;
$body = “Hi,\n\nHow are you?”;
mail($to, $subject, $body);[/php]

If that works you know the issue is with something you’re passing in …


#12

There’s something wrong with the things passing in because that worked

Thank you!

I’ll check if I can find what’s the problem. Could you possibly check the code to?

Thanks in advance,
Busarna


#13

Just narrow it down…

Comment out the line

  //$to = "tma.s@hotmail.com";

See if it works…

Then comment out

//$subject = "Hi!";

Rinse repeat and narrow down the issue.


#14

Okay I’ll narrow it down. Thanks


#15

I’ve narrowed down the issue now. The problem is something about the message that’s in HTML format.

Any ideas of what the problem could be?

Thanks in advance,
Busarna


#16

I would make the changes below, you’re sending email I think you have a lot of overkill which might be causing your issues.

Change:

[php] $message = ‘Busarna4.pusku.com Message

busarna4.pusku.comBusarna4.pusku.com Account Activation
Hello ‘.$u.’,

Click the link below to activate your account when ready:

Click here to activate your account now

Login after successful activation using your:
* E-mail Address: ‘.$e.’
’;[/php]

To:

[php] $message = ‘

busarna4.pusku.comBusarna4.pusku.com Account Activation
Hello ‘.$u.’,

Click the link below to activate your account when ready:

Click here to activate your account now

Login after successful activation using your:
* E-mail Address: ‘.$e.’
’;[/php]