Jump to content

println

New Members
  • Posts

    2
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

println's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I fixed your code, here are the errors i found 1)If you are gonna call $_SESSION, then you must call session_start() 2)You forgot to select the Database to use, (i.e forgot to call mysql_select_db) 3)In your logout link, you say go to "process.php" but how is process.php supposed to know which function you want to use? To remedy this i modified the url process.php?action=logout . Now the process.php can check if the url contains a action key in $_GET and check it's value is "logout" and then it can logout the user. Also, the main purpose of classes is to group related methods and variables, so the class can be reused as many times as need in the form of objects mainly ($p = new class(), $p is an object,) config.php <?php //Configuration File session_start(); define("DB_HOST","localhost"); //Define Host define("DB_USER","root"); //Define Username define("DB_PASS",""); //Define Password define("DB","practice"); //Define the db define("DB_TABLE","user"); //Define the db //Connect to the Server $connection = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die (mysql_error()); //Connect to the Databse mysql_select_db(DB, $connection); ?> functions.php <?php include ("config.php"); //Classes and functions! class memberssystem { function check_login($username,$password) { $result = mysql_query("SELECT * FROM user WHERE username ='$username'") or die(mysql_error()); $check_rows = mysql_num_rows($result) or die(mysql_error()); if($check_rows != 1) { return false; } else { while($row = mysql_fetch_array($result)) { if($row['username'] != $username || $row['password'] != $password) { return false; } else return true; //Success! } } } public function logout() { session_destroy(); header("location: index.php"); die(); } } $members = new memberssystem(); ?> index.php <!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" /> <title>Login Page!</title> </head> <body> <?php session_start(); if(isset($_SESSION['username'])) { //Already logged in member! header("location: members.php"); } ?> <table> <form action='process.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> <input type='submit' value='login' name='submit' /> </td> </tr> </form> </table> </body> </html> members.php <?php session_start(); //Member's Page! if(!$_SESSION['username']) { header("location: index.php"); } ?> <html> <h1>Member's Page!</h1> <p>Welcome to the Member's Page! For right now, you can not do anything :/ but you can <a name='submit_logout' href='process.php?action=logout'>logout</a> :} </p> </html> process.php <?php include("functions.php"); //Process for each form! if(isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $login = $members->check_login($username,$password); if($login) { $_SESSION['username'] = $username; header("location: members.php"); die(); } } if(isset($_GET['action']) && $_GET['action'] == 'logout') { $members->logout(); } ?>
  2. Ok, the first section is good. Now, the section below, your check_login function returns a 1 or true depending on if the user/pass match. Now the var of the object, $username, and $password do not get initialized during the check_login function. In your login function your are passing in null values to check_login by sending $this->username and this->password (null values). Also as a side note, if you already validate the user in check_login, why do it again in the login function ? <?php include ("config.php"); //Classes and functions! class memberssystem { var $username, $password; function check_login($username,$password) { $result = mysql_query("SELECT * FROM member WHERE username ='$username'") or die(mysql_error()); $check_rows = mysql_num_rows($result) or die(mysql_error()); if($check_rows != 1) { return 0; } else { while($row = mysql_fetch_array($result)) { if($row['username'] != $username || $row['password'] != $password) { return 1; } else return true; //Success! } } } public function login() { if($this->check_login($this->username,$this->password) == 0) { die("Username doesn't exist!"); } else if($this->check_login($this->username,$this->password) == 1) { die("Sorry but the combination doesn't match"); } else { $_SESSION['username'] = $this->username; header("location: members.php"); } } public function logout() { session_destroy(); header("location: index.php"); } } $members = new memberssystem; ?>
×
×
  • 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.