Jump to content

[SOLVED] Login Problem


Tiff

Recommended Posts

Ive been working on a login session for my college work, but i have one problem, it doesnt work, i was wondering if anyone could help me with it, i will post my code below, and if you could reply and tell me what you think is wrong with it then i would be more than greatfull

 

<?php
error_reporting(E_ALL);
if (!isset($FirstName) || !isset($Password))
{
header( "Location: index.htm" );
}

else

if (empty($FirstName) || empty($Password)) 
{
header( "Location: index.htm" );
}

else

{
$user = ($_POST['FirstName']);
$pass = ($_POST['Password']);

$host="localhost";
$db_user="chris_root"; 
$db_password="?"; 
$database="chris_mydb"; 

$db = mysql_connect("localhost","chris_root","?") or die ("Error connecting to database.");

mysql_select_db("chris_mydb", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from person where FirstName='$user' AND Password='$pass'", $db);

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

  session_start();
  session_register('FirstName');

  header( "Location: Template.php" );

  }

  }
else {

  header( "Location: index.htm" );
  
}
}
?> 

 

Thanks

 

Chris

Link to comment
Share on other sites

Please explain more. What do you mean by it doesnt work?

 

For a start this:

<?PHP
error_reporting(E_ALL);
if (!isset($FirstName) || !isset($Password))
{
header( "Location: index.htm" );
}

else

if (empty($FirstName) || empty($Password))
{
header( "Location: index.htm" );
}

isnt correct because the else doesnt have { }

Link to comment
Share on other sites

Hey i would just like to add

 

When you said the else is missing the {}

 

Its now come up with an error now i have added them

 

Parse error: syntax error, unexpected T_ELSE in /home/chris/public_html/userlogin.php on line 16

 

Im all new to PHP and what not

 

Thanks if you can help

 

<?PHP

error_reporting(E_ALL);

if (!isset($FirstName) || !isset($Password[/color))

{

header( "Location: index.htm" );

}

 

else{

 

if (empty($FirstName) || empty($Password))

{

header( "Location: index.htm" );

}

}

Link to comment
Share on other sites

I've sorted the original problem you told me about and now it isnt doing much more than it was the first time

 

<?PHP
error_reporting(E_ALL);
if (empty($FirstName) || empty($Password)) 
{
header( "Location: index.htm" );
}

else{

if (!isset($FirstName) || !isset($Password))
{
header( "Location: index.htm" );
}

$user = ($_POST['FirstName']);
$pass = ($_POST['Password']);

$host="localhost";
$db_user="chris_root"; 
$db_password="?"; 
$database="chris_mydb"; 

$db = mysql_connect("localhost","chris_root","?") or die ("Error connecting to database.");

mysql_select_db("chris_mydb", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from person where FirstName='$user' AND Password='$pass'", $db);

$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

  session_start();
  session_register('FirstName');

  header( "Location: Template.php" );

  }

  }
else {

  header( "Location: index.htm" );
  
}
}
?> 

Link to comment
Share on other sites

It's hard to debug a script that is redirecting instantly, if you don't remove the redirect to check for warnings.

 

<?php
error_reporting(E_ALL);
if (empty($FirstName) || empty($Password) || !isset($FirstName) || !isset($Password)) 
{
echo "I would of sent you back to login, because you forgot to type something.";
//header( "Location: index.htm" );
}
else{

$user = ($_POST['FirstName']);
$pass = ($_POST['Password']);
$db = mysql_connect("localhost","chris_root","?") or die ("Error connecting to database.");
mysql_select_db("chris_mydb", $db) or die ("Couldn't select the database.");
$result=mysql_query("select * from person where FirstName='$user' AND Password='$pass'", $db);
$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
session_start();
session_register('FirstName');
echo "Login correct. I would of loaded Template.php";
//header( "Location: Template.php" );
  }
else {
echo "Invalid login.";
//header( "Location: index.htm" );  
}
}

 

I also cleaned up some redudancy and whitespace.

Link to comment
Share on other sites

I would also like to ask:

 

I masked the password with the HTML code:

 

<input type="password" name="Password" mask="x">

 

Would this bother the PHP code in anyway when trying to read it?

Link to comment
Share on other sites

For starters, you do not need a loop if your only expecting one record. Secondly, session_register() has long been depricated. Thirdly, you never assign anything to any session variable (log user in).

 

Change this....

 

if($rowCheck > 0){
while($row = mysql_fetch_array($result)){

  session_start();
  session_register('FirstName');

  header( "Location: Template.php" );

  }

  }
else {

  header( "Location: index.htm" );
  
}
}

 

to....

 

if ($rowCheck > 0) {
  $row = mysql_fetch_array($result);
  session_start();
  $_SESSION['FirstName'] = $user;
  header( "Location: Template.php" );
} else {
  header( "Location: index.htm" );
}

Link to comment
Share on other sites

ok thanks, but i still have the same problem, and i cant work it out

 

ever get something like that? its gets on your nerves doesnt it, anyway lets explain it;

 

You input your firstname and your password into the Index page after you have signed up, and then you click the login button, and as i have had some help they made it so it tells me that theres something missing instead of instantly redirecting me, so it keeps saying that i forgot to put something either in the "FirstName" or "Password" box if you get me..

 

But anyway, its added the user and the file to the data base but it just tells me that theres something missing, so i dont know what to do, are there any helpful suggestions you can think of that might help?

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.