Jump to content

Auto Login after registering


Deanznet

Recommended Posts

Okay.. after a user registers i want them to be automatically be loged in and taken to info.php page..

 

This is what i got for the register page.

 

//Okay so if everything goes good time to insert!
mysql_query("INSERT INTO `pbs_users` (uUsername,uPassword,uEmail,uFName,uLName,uCountry) VALUES ('{$email}','{$password}'),'{$email}','{$firstname}'),'{$lastname}'),'{$country}')") or die("Could not create account<br/><b>Error:</b>".mysql_error());
die('Account created!<br/><a href="usercp.php?login">Click Here</a> to login!');

	echo '<meta http-equiv="refresh" content="1;url=info.php">';

 

Okay so what do i have to do to make it login automatically

 

info.php

 


<?php
//required files
require_once(ROOTDIR.'lib/checklogin.php');
require_once('../lib/config.php');


echo "<h5 class=\"specialH5\">YOU ARE LOGED IN</h5>";  
?>




checklogin.php

<?PHP


session_start();

function random($max) {
srand( (double)microtime() * 1000000 );
$r = round(rand(0, $max));
if ($r != 0) $r = $r - 1;
return $r;
}

if (!defined("LOADED_PROPERLY")) 
{
$errormessage = 'License did not load correctly.';
include(ROOTDIR.'lib/template.php');
}
$entered_login = $_REQUEST['user'];
$entered_password = $_REQUEST['pass'];

if (!$entered_login && !$entered_password && $_SESSION['expire']>time()) 
{
$login = $_SESSION['login'];
$password = $_SESSION['password'];
} else {
session_unregister('login');
session_unregister('password');
$newsession = 1;
$login = $entered_login;
$password = md5($entered_password);
$_SESSION['login'] = $login;
$_SESSION['password'] = $password;
}

if (!$login) {
if(!empty($_REQUEST['log'])){ $errormessage = ERR_NOUSER; }
include(ROOTDIR.'lib/template.php');
exit;
}
if (!$password) {
if(!empty($_REQUEST['log'])){ $errormessage = ERR_NOPASSWORD; }
include(ROOTDIR.'lib/template.php');
exit;
}

$userQuery = mysql_query('SELECT * FROM '.$TbUsers->tblName.' WHERE '.$TbUsers->col_uEmail.' = \''.$login.'\'');
if (mysql_num_rows($userQuery) != 0) 
{
$userArray = mysql_fetch_array($userQuery);
if ($login != $userArray[$TbUsers->col_uUsername]) 
{
	//username doesn't exist in db
	if(!empty($_REQUEST['log'])){ $errormessage = ERR_WRONGUSER; }
	include(ROOTDIR.'lib/template.php');
	exit;
}
} else {
//username doesn't exist in db
if(!empty($_REQUEST['log'])){ $errormessage = ERR_WRONGUSER; }
include(ROOTDIR.'lib/template.php');
exit;
}
if (!$userArray[$TbUsers->col_uPassword]) 
{
//password doesn't exist in db
if(!empty($_REQUEST['log'])){ $errormessage = ERR_WRONGPASS; }
include(ROOTDIR.'lib/template.php');
exit;
}
if (stripslashes($userArray[$TbUsers->col_uPassword]) != $password) 
{
//password is incorrect in db
if(!empty($_REQUEST['log'])){ $errormessage = ERR_WRONGPASS; }
include(ROOTDIR.'lib/template.php');
exit;
}
if ( isset($userArray[$TbUsers->col_uIsAdmin])) 
{
if($_REQUEST['log'] == 1)
{
	$usuUsername = checkUsername($login);
	$usuUsername = $usuUsername[$TbUsers->col_uId];
	$usIp = $_SERVER['REMOTE_ADDR'];
	$usDate = date('YmdHis');  
	mysql_query("insert into ".DBPREFIX."_userlog (usId, usuId, usIp, usDate) VALUES ('', '$usuUsername', '$usIp', '$usDate')");
}
$userLevel = stripslashes($userArray[$TbUsers->col_uIsAdmin]);
if($newsession==1) 
{
	switch($userLevel)
	{
		case '1':
			if(($userArray[$TbUsers->col_uRemember]==1)||($_REQUEST['rememberme']==1))
			{ 
				$_SESSION['expire']=time()+14*24*60*60;
			} else {
				$_SESSION['expire']=time()+$configData[$TbConfig->col_conSiteCookie];
			}
			break;

		case '2':
			$_SESSION['expire']=time()+$configData[$TbConfig->col_conSiteCookieAdmin];
			break;

		case '4':
			$_SESSION['expire']=time()+1200;
			break;
	}
}
}
if ( ( $requiredUserLevel && !empty($requiredUserLevel[0]) ) || $minUserLevel ) 
{
if ( ( !in_array($userLevel, $requiredUserLevel) && ( !isset($minUserLevel) || empty($minUserLevel) || $userLevel < $minUserLevel ) ) ) 
{
	//wrong user level
	if(!empty($_REQUEST['log'])){ $errormessage = ERR_WRONGLEVEL; }
	include(ROOTDIR.'lib/template.php');
	exit;
}
}
if ( isset($userArray[$TbUsers->col_uId]) ) 
{
$ID = stripslashes($userArray[$TbUsers->col_uId]);
$UINFO = $userArray;
define('CHECKEDLOGIN',true);
}
//define('BUG_REPORT','Current time: '.time().' Cookie expiration: '.$_SESSION['expire']);
?>

 

Link to comment
https://forums.phpfreaks.com/topic/153782-auto-login-after-registering/
Share on other sites

i store a user token in the session. if the token exists, the user has been logged in. not sure how you're implementing this, but doing it this way makes it easy to "login" the user post-registration by merely creating the token in the session. in my case, the token is a serialized User object.

 

jason

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.