Problems with login form.

Hi. I’m having this days some problems to a login form. I connected my php to a MySQL database.

The problem is when I log into the site. The login form should verify my datas and redirect me to a PHP page reserved only to logged people but it now works.

This is a part of the PHP code:
[php]<?php
include (“include/functions.php”);
include (“resources/sqlconnector.php”);
session_start();
session_regenerate_id(TRUE);
if(isset($_SESSION[‘username’]) || (isset($_COOKIE[‘username’]) && isset($_COOKIE[‘pass’])))
{
header(‘location:reserved.php’);
exit;
}
if (isset($_POST[‘username’]) && isset($_POST[‘pass’]) && strlen($_POST[‘username’]) > 0 && strlen($_POST[‘pass’]) > 0)
{
$name = htmlspecialchars($POST[‘username’]);
for($i = 0; $i < strlen($name); $i++) if($name[$i] == ’ ') $name[$i] = '
’;
$pass = num_hash(htmlspecialchars($_POST[‘pass’]));
$query = “SELECT * FROM users WHERE LOWER(Name) = LOWER(’$name’) AND password_hash = $pass”;
$result = mysql_query($query);
if($result)
{
if($row = mysql_fetch_array($result))
{
if($row[‘Banned’] == 1)
{
echo"You are banned!";
}
else
{
$_SESSION[‘username’] = $name;
$_SESSION[‘pass’] = $pass;
if(isset($_POST[‘remember’]))
{
setcookie(‘username’, $name);
setcookie(‘pass’, $pass);
}
echo"You are logged in!";
header(‘location:reserved.php’);
exit;
}
}
else
{
echo “Retype Username and Password”;

		}	
	}
	else
	{
		exit('<b>Unable to execute the query</b>');
	}
}	

?>[/php]

When I click on the input “submit”, it refresh and I can’t log in. Can anyone help me?

Are you able to please show your form?

The entirely code page or the compiled hosted page?

The form that submits the login information.

Just that.

I didn’t use the form tags. This is the code:

[php]














Username:

Password:

Remember?
[/php]

OK. Now it works. I added the form tags.

Now I have a problem.

This is the code:
[php]<?
if($_SESSION[“logged”])
{
echo("Logged in as " + $_SESSION[‘username’]);
}
else
{
echo(“not connected.”);
}?>[/php]

It should says Account Status: Logged in as xxxxx or not connected but it says only “Account status: not connected” or, if I login, “Account Status: 0” or “Account Status:”

Ensure you resume the session with session_start();

[php]<?php
session_start();

        if($_SESSION["logged"])
		{
			echo("Logged in as " + $_SESSION['username']);
		}
		else
		{
			echo("not connected.");
		}?>[/php]

It says only “not connected.” or “0”. :frowning:

Hi Corsaro,

Make sure you have properly set the METHOD and ACTION attributes in your . Must be like:

...

I noticed you’ve pointed your input/submit control into a URL and I think that’s not a good idea.

I’ve set POST and METHOD into the form tags.

This is the tag:
[php][/php]

Change this

if($_SESSION[“logged”])

to this

if($_SESSION[“username”])

At least I think that’s right, as I couldn’t see where you’re setting the “logged” session variable.

Or, set the “logged” session variable and then your original code should work. Hopefully

It says only “not connected”…

New bug: sometimes when I login the website says “Unable to execute the query”. I don’t know how to fix this issue. If you want the code, I’ll post it.

Sponsor our Newsletter | Privacy Policy | Terms of Service