Jump to content

Function Help.


cyimking

Recommended Posts

Im working on a simple login script. I had it working correctly the first time, but i was told that if i made the code into a function then it will benefit me in the long term... So i tried to do just that, and regardless of what happens it do not work. When the user click the submit button , he is going into a process file, which will process his login information and if correct, then he will be directed back to the home screen. But what really happens is that, the user is redirected to the process.php file with blank data...

 

I know there an error somewhere in here.

 

process.php

<?php

include_once "../config.php";
include_once "../login.php";

//Process all the information so that everything is nice and fine!


class Process
{
var $username;
var $password;
var $id;

function Process()
{

if(isset($_POST['sublogin']))
{
           $this->prologin();
}

 else if(isset($_POST['sublogout']))
{
	$this->prologout();
}

      else 
  header("location: ../index.php");
}


function prologin()
{

$check_login = $this->login($_POST['username'],$_POST['password']);

if($check_login)
{
	header("location: ../index.php");
}

else
 $er = "Incorrect Login Information!";
}

function login($username,$password)
{
	$this->password = $this->md5(password);
	$sql = mysql_query("SELECT * FROM members WHERE username = '$username' and password = '$password'");
	$check_sql = mysql_num_rows($sql);

	if($check_sql != 1)
	{
		$er = "User does not exist.";
	}

	else 
	{

		while($row = mysql_fetch_array($sql))
		{
			$this->id = $row['id'];
			$_SESSION['id'] = $this->id;
			$_SESSION['username'] = $this->username;
			return true;

		}

	}

}


}

$process = new Process;

?>

 

login

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />

<title>Welcome Back</title>
</head>

<body>
<?php include_once "header.php"; ?>

<h1><center>Welcome Back, Please Sign In.</center></h1>
<center><?php echo $er; ?></center>
<div class ='login'>
<div class='login_inside'>
<div class='login_header'>
Login
</div>

<table class='login_table'>
<form action='scripts/test.php' method='POST'>
<tr>
<td>Username: </td>
<td><input type='text' name='username' /></td>
</tr>

<tr>
<td>Password: </td>
<td><input type='password' name='password' /></td>
</tr>

<tr>
<td></td>
<td><input type='submit' name='sublogin' value='login'  /></td>
</tr>
</form>
</table>
</div>
</div>

</body>
</html>

Link to comment
Share on other sites

In the function prologin() you use the login function to validate if the user exists (fine).

In the login() function you do not return any value thats useable for the prologin() function.

Try returning 0 or 1 in the login() function, if the user exists or not.

Link to comment
Share on other sites

the forms action shown is "scripts/test.php", not process.php

Be a little more descriptive as to what exactly is happening.

 

Well that is where its going. The name is under test.php because im testing it, which runs all the code. So process.php is currently empty, i just used that name.

 

@creata.physics - Where would i put that code at?

 

@Niixie - What will the 0 or 1 do? I set it so if $check_login == 0, redirect to the index.php page, which should have the user logged in. BUT this is not what is happening. Regardless of what i do, it is going to the test.php (process.php, same thing) in a blank page. The only code that is working, is if a user goes there by accident, then it redirect him page to the index.php page.

Link to comment
Share on other sites

Notice: Undefined variable: er in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\empora\login.php on line 55 on the login.php one...

 

Fatal error: Call to undefined method Process::md5() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\empora\scripts\test.php on line 49 on the test.php one.

Link to comment
Share on other sites

1. Refer to thorpe's first reply for reference to the first error.

 

2. The second error is caused by this line:

 

$this->password = $this->md5(password);

 

the function md5() is not derived from the Process class, but a PHP native function, so it should be just:

 

$this->password = md5($password);

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.