Jump to content

[SOLVED] Sessions


Akenatehm

Recommended Posts

Hey Guys,

 

This script doesn't seem to be creating and checking sessions properly. When I click submit, it just resets the page.

 

Heres the functions I'm calling from functions.php:

function startusersession()
{
session_start();
$_SESSION["loggedin"];
}

function endsession()
{
foreach($_SESSION as $key => $val) unset($_SESSION[$key]);
}

function checksession()
{
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'])
{
header("Location:alreadyloggedin.php");
}
}

 

and here's login.php

<?php
include 'functions.php';
include 'connect.php';
checksession();
if (isset($_POST['submit']))
{	
if($_POST['password'] != $_POST['confirmpassword'])
{
	$result = '<font color="red">Passwords Do Not Match</font>';
}
else{
$checkuserexists = mysql_query("SELECT * FROM accounts WHERE username = '$username'");
if(mysql_num_rows($checkuserexists) > 0)
{
$result = '<font color="red">User Does Not Exist</font>';
}
else
{
	$usersdetails = mysql_fetch_assoc($checkuserexists);
	$usersusername = $usersdetails['username'];
	$userspassword = $usersdetails['password'];
	$formusername = $_POST['username'];
	$formpass = $_POST ['password'];

	if($formusername != $usersusername || $formpass != $userspassword)
	{
	$result = "Incorrect Username or Password";
	}		
	else
	{
	startusersession();
	headers("Location:loggedin.php");
	}

}
}
}
?>


<html>
<head>
<title>Login - Fall of The Empire</title>
<link rel="stylesheet" href="styles.css" type="text/css"/>
</head>
<body bgcolor="272d31">
	<div align="center" class="header">
		<img src="images/logo.png" width="473" height="227" alt="Logo">
</div>
<br>
<center>
<div class="main">
<div class="navbar">
	<img src="images/nav_top.png" width="232" height="46" alt="Nav Top"><br>
	<a href="index.php"><img src="images/nav_home.png" width="232" height="33" alt="Nav Home" border="0"></a><br>
	<a href="news.php"><img src="images/nav_news.png" width="232" height="32" alt="Nav News" border="0"></a><br>
	<a href="about.php"><img src="images/nav_about.png" width="232" height="23" alt="Nav About" border="0"></a><br>
	<a href="login.php"><img src="images/nav_login.png" width="232" height="32" alt="Nav Login" border="0"></a><br>
	<a href="register.php"><img src="images/nav_register.png" width="232" height="25" alt="Nav Register" border="0"></a><br>
	<a href="contactus.php"><img src="images/nav_bottom.png" width="232" height="179" alt="Nav Bottom" border="0"><br></a>
	</div>	

	<div class="cb">
	<img src="images/login.png" align="right" class="welcome">
	<br>
	<br>
	<br>
	<br>
	<table width="300px" align="right">
		<form action="<?php echo $PHP_SELF; ?>" method="POST">
			<tr>
				<td>
					<font color="white">
						Username:
					</font>
				</td>

				<td>
					<input type="text" name="username">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Password:
					</font>
				</td>

				<td>
					<input type="password" name="password">
				</td>
			</tr>

			<tr>
				<td>
					<br>
				</td>
				<td>
				</td>
			<tr>
				<td>
				</td>
		<td align="right" style="padding-right:8px">
		<input type="submit" name="submit" value="Login"align="right">
		</td>
		</tr>
		</form>	
	</table>
	<p><?php echo $error; ?></p>
	</div>
	</div>
	</center>
</body>
</html>

 

Help would be great!

Link to comment
Share on other sites

hello again bro :)

 

try doing session_start() before you include the files.. then just use $_SESSION inside the functions

 

because in order to fill the $_SESSION superglobal you need to actually start the session otherwise $_SESSION will not be present..

Link to comment
Share on other sites

Hey Lol.

 

I found out their were a few reasons why it wasn't working.

 

Firstly, I didn't set the variable $username to be checked before it was checked.

 

Secondly, I had set a 20 char limit in the table to the password field so it was only accepting 20 of the should be 32 md5 pass chars ther shud have been.

 

After fixing, it is algud.

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.