Jump to content

Staying logged in


dean7

Recommended Posts

Hey all, I stopped coding for some time but I've only just decided to restart coding but already hit into something I can't get around.

 

I've got my Login script for which you will login to on my game. Once logged in you can play untill you logout although, I can login fine but as soon as I get to the first page on my site it sends me back to the login page as it thinks I'm not logged in.

 

Here is my Login Script:

 

Index.php

 

<?php
session_start();
include_once ("TheDbs/ConfigerationFile.php");
if (isset($_GET['logout']) && strip_tags($_GET['logout']) == "yes") {
session_destroy();
}elseif (isset($_SESSION['username'])){
header("Location: GameIndex.php");
exit();
}
if (strip_tags($_POST['Submit']) && strip_tags($_POST['username']) && strip_tags($_POST['password'])){
$username = strip_tags(mysql_real_escape_string($_POST['username']));
$password = strip_tags(mysql_real_escape_string($_POST['password']));
$select = mysql_query("SELECT * FROM $TableUsersInfo WHERE online > '$timenow' ORDER by rank desc") or trigger_error("Query Error, Line 16 . " . mysql_error());
$num = mysql_num_rows($select);
$date = date('D, jS M, Y, g:ia');
$ip = getenv("REMOTE_ADDR") ;
if((!$username) || (!$password)){
$message="<br /><table width='30%' align='center' class='TableStyle' border='1' cellpadding='0' cellspacing='0'>
<tr>
<td class='error_header' align='center'>Error</td>
</tr>
<tr>
<td align='center'><strong>Please fill in all fields.</strong></td>
</tr>
</table>";
}
///check INFO
$sql = mysql_query("SELECT * FROM $TableUsers WHERE username='".mysql_real_escape_string($username)."' AND password='".mysql_real_escape_string($password)."' LIMIT 1") or trigger_error("Query Fail, Line 26. " . mysql_error());

$login_check = mysql_num_rows($sql);
$infomation = mysql_real_escape_string(mysql_fetch_object($sql));
if ($login_check == "0"){
$message="<br /><table width='30%' class='TableStyle' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr>
<td class='error_header' align='center'>Error</td>
</tr>
<tr>
<td align='center'><strong>Your Username or Password is incorrect!</strong></td>
</tr>
<table>";
}elseif ($login_check != "0"){

if ($login_check > "0"){
if ($infomation->status == "Banned"){
$encoded=md5(strtolower($username));
header("Location: BannedUser.php?$encoded=user$banned");
exit();
}

 session_register('username');
 $_SESSION['username'] = $information->username;



	 $timestamp = time()+20;
	 $get = mysql_query("SELECT * FROM $TblUsers WHERE username='".mysql_real_escape_string($username)."' LIMIT 1");
	 $got = mysql_fetch_object($get);

mysql_query("UPDATE $TableUsersInfo SET online='".mysql_real_escape_string($timestamp)."' WHERE username='".mysql_real_escape_string($username)."' LIMIT 1");
mysql_query("UPDATE $TableUsers SET login_ip='".mysql_real_escape_string($ip)."' WHERE username='".mysql_real_escape_string($username)."' LIMIT 1");	
mysql_query("UPDATE $TableUsers SET lastlogin='".mysql_real_escape_string($date)."' WHERE username='".mysql_real_escape_string($username)."' LIMIT 1");
 header("Location: GameIndex.php");


} else {
$message= "<br /><table width='30%' class='TableStyle' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr>
<td class='error_header' align='center'>Error</td>
</tr>
<tr>
<td align='center'><strong>Error While Trying to Login!</strong></td>
</tr>
<table>";

}}}
?>
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.ThisBody {
background-color: #737373;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
font-family: Verdana;
font-size: 11px;
font-style: normal;
color: #FFFFFF;
}
</style>
<link rel='stylesheet' href='includes/style.css' type='text/css'>
</head>
<form action='' method='post'>
<br />
<body class='ThisBody' leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?php echo ($message); ?>
<table id="Table_01" align='center' width="530" height="292" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="7" background="images/LoginScreen/Indexright.png" height='220' align='center'><br /><br />
<img src='images/LoginScreen/Username.png'><br /><input name="username" class='TextInput' type="text" maxlength="15" id="username"><br />
	 <img src='images/LoginScreen/Password.png'><br /><input name="password" class='TextInput' type="password" maxlength="30" id="password"><br /><br />
	 <input type="submit" class='Button' name="Submit" value="Login SC">
	 </td>
</tr>
</table>
</form>
<br />
<table align="center" cellpaddig='0' cellspacing='0' border='1' width="35%" class='TableStyle'>
<tr>
<td class='UnderTable' align='center'>Help:</td>
<tr>
<td colspan="2" width="100%" align="center"><a href="Register.php">Register</a> - <a href="TOS.php">Terms Of Service</a> - <a href="LostPassword.php">Lost Password</a></td>
</tr>
</table>

 

And simply on my gameindex page its a simple piece of code for the moment:

 

GameIndex.php

<?php
session_start();
include ("TheDbs/ConfigerationFile.php");
include ("TheDbs/GameFunctions.php");
logincheck();
$Username = $_SESSION['username'];
?>
Game Index

 

And the last thing is the function logincheck() is another reasonably simple code:

 

function logincheck(){

if (empty($_SESSION['username'])){
echo "
<SCRIPT LANGUAGE='Javascript'>
window.location='Index.php';
</script>
";
exit();
}}

 

Thanks for any help! =D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.