Jump to content

php remember me


chriscloyd

Recommended Posts

Okay my php remember me is not working could someone help me heres the scripts that im running

login.php

login_form.php

check_rememberme.php

index.php

 

login.php

<?php
session_start();
include("config.php");
$username = $_POST['username'];
$password = md5($_POST['password']);
$check_user = mysql_query("SELECT * FROM cegl_users WHERE username = '$username'");
if ($check_user) {
$get_num_user = mysql_num_rows($check_user);
if ($get_num_user > 0) {
	$userinfo = mysql_fetch_array($check_user);
	if ($password == $userinfo['password']) {
		$_SESSION['cegl_username'] = $username;
		$_SESSION['cegl_userid'] = 	$userinfo['user_id'];
		if (isset($_POST["remember"])) {
			setcookie("cegl_username", "$username", time() + (60*60*24*30));
			setcookie("cegl_password", "$password", time() + (60*60*24*30));
			setcookie("cegl_autologin", "yes", time() + (60*60*24*30));
		}
		if ($userinfo['level'] == 'admin') {
				$get_admin = mysql_query("SELECT * FROM cegl_admins WHERE user_id = '".$_SESSION['celg_username']."'");
				if ($get_admin) {
					$admininfo = mysql_fetch_array($get_admin);
					$_SESSION['cegl_admin'] = 'yes';
					$_SESSION['cegl_level'] = $admininfo['type'];					
				}
				header("Location: ../admin.php");
			}
		header("Location: ../index.php");
	} else {
		$reasonLOGIN = '-Wrong Information';
		header("Location: ../index.php?p=login&reasonLOGIN=$reasonLOGIN");
	}
} else {
	$reasonLOGIN = '-Wrong Information';
	header("Location: ../index.php?p=login&reasonLOGIN=$reasonLOGIN");
}
}
?>

 

login_form.php

<?php
include("config.php");
if(isset($_SESSION['cegl_username'])) {
$get_userinfo = mysql_query("SELECT * FROM cegl_users WHERE username = '".$_SESSION['celg_username']."' ");
$user = mysql_fetch_array($get_userinfo);
//get messages
$get_messages = mysql_query("SELECT * FROM cegl_messages WHERE to_id = '".$user['user_id']."'");
$count_messages = mysql_num_rows($get_messages);
echo '<div align="center">Welcome '.$_SESSION['cegl_username'].'<br />
You Have <span class="lostpass"><a href="index.php?p=viewmessages" class="lostpass">('.$count_messages.')</a></span> Messages<br />
<span class="register"><a href="index.php?p=profile" class="register">Profile</a></span> <span class="register"><a href="index.php?p=supportdesk" class="register">Support Desk</a></span> <span class="lostpass"><a href="files/logout.php" class="lostpass">Logout</a></span> ';
if (isset($_SESSION['cegl_admin'])) {
echo ' <span class="register"><a href="admin.php" class="register">Admin CP</a></span>';
}
echo '</div>';
} else {
echo '<form name="form1" method="post" action="files/login.php">
<div align="center">
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="39%"><input name="username" type="text" class="loginarea" id="username" value="Username"></td>
<td width="40%"><input name="password" type="password" class="loginarea" id="password" value="password"></td>
<td width="21%"><input name="Submit" type="submit" class="loginbutton" value="Login"></td>
</tr>';
if (isset($_GET['reasonLOGIN'])) {
echo '<tr>
<td class="white" colspan="2">'.$_GET['reasonLOGIN'].'</td>
</tr>';
}
echo '<tr>
<td align="left"><div align="center" class="style1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input name="remember" type="checkbox" id="remember" value="rememberme" checked>                        </td>
<td align="left" class="lostpass">Remember</td>
</tr>
</table>                </td>
<td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="register"><a href="index.php?p=register" class="register">Register</a></td>
<td class="lostpass"><a href="index.php?p=lostpass" class="lostpass">Lost Password</a></td>
</tr>
</table></td>
</tr>
</table>
</div>
</form>';
}
?>

 

check_rememberme.php

<?php
include("config.php");
if (isset($_COOKIE['cegl_autologin'])) {
$username = $_POST['cegl_username'];
$password = $_POST['cegl_password'];
$check_user = mysql_query("SELECT * FROM cegl_users WHERE username = '$username'");
if ($check_user) {
	$get_num_user = mysql_num_rows($check_user);
	if ($get_num_user > 0) {
		$userinfo = mysql_fetch_array($check_user);
		if ($password == $userinfo['password']) {
			$_SESSION['cegl_username'] = $username;
			$_SESSION['cegl_userid'] = 	$userinfo['user_id'];
			if ($userinfo['level'] == 'admin') {
					$get_admin = mysql_query("SELECT * FROM cegl_admins WHERE user_id = '".$_SESSION['celg_username']."'");
					if ($get_admin) {
						$admininfo = mysql_fetch_array($get_admin);
						$_SESSION['cegl_admin'] = 'yes';
						$_SESSION['cegl_level'] = $admininfo['type'];					
					}
				}
		}
	}
}
}
?>

 

index.php

<?php
session_start();
include("files/check_rememberme.php");
?>
<br>
<br>
<?
include("files/check_rememberme.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/45011-php-remember-me/
Share on other sites

There's a error in there,

 

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

$username = $_POST['cegl_username'];

$password = $_POST['cegl_password'];

 

The $_POST stuff will be empty, because it was not POST'd.

You might want to access the $_COOKIE['cegl_username'] and password - although I don't recommend storing password in cookie files.

Link to comment
https://forums.phpfreaks.com/topic/45011-php-remember-me/#findComment-218496
Share on other sites

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.