cyimking Posted March 24, 2012 Share Posted March 24, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/ Share on other sites More sharing options...
trq Posted March 24, 2012 Share Posted March 24, 2012 $er does not exists outside of your class. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1330824 Share on other sites More sharing options...
creata.physics Posted March 25, 2012 Share Posted March 25, 2012 @thorpe - regardless of $er not existing outside of the class it wouldn't create a blank page. that's would just be a notice issue. @cyimking - can you turn on error reporting and tell me what you get? ini_set('display_errors',1); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1330838 Share on other sites More sharing options...
AyKay47 Posted March 25, 2012 Share Posted March 25, 2012 the forms action shown is "scripts/test.php", not process.php Be a little more descriptive as to what exactly is happening. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1330847 Share on other sites More sharing options...
trq Posted March 25, 2012 Share Posted March 25, 2012 @thorpe - regardless of $er not existing outside of the class it wouldn't create a blank page. that's would just be a notice issue. There is no mention of a blank page in the op's post. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1330868 Share on other sites More sharing options...
Niixie Posted March 25, 2012 Share Posted March 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1330888 Share on other sites More sharing options...
cyimking Posted March 25, 2012 Author Share Posted March 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1331070 Share on other sites More sharing options...
creata.physics Posted March 25, 2012 Share Posted March 25, 2012 If every one of your pages requires config.php to operate then go ahead and add the code to the top of config.php Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1331075 Share on other sites More sharing options...
cyimking Posted March 26, 2012 Author Share Posted March 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1331083 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1331089 Share on other sites More sharing options...
cyimking Posted March 26, 2012 Author Share Posted March 26, 2012 Ok that solves that problem. Now what is not happening, is that its not logging in the member. Although all my information are correct.. Quote Link to comment https://forums.phpfreaks.com/topic/259655-function-help/#findComment-1331230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.