jont Posted October 31, 2009 Share Posted October 31, 2009 I'm trying to get to grips with classes, could someone maybe convert my login function to a class to give me a basic idea/example? /* * @function login * display the login form or process it if it's been submitted */ function login() { $db = new db; $db->connect(); // connect to database // if the form has been submitted start processing it if(isset($_POST['submit'])) { $username = $_POST["username"]; // get the username $password = $_POST["password"]; // get the password $error = ''; // no errors $validinput = new validinput; // start the valid input class // check the username and password are valid, if not add a warning message to $error $error .= $validinput->check('username', $username); $error .= $validinput->check('password', $password); // if theres any errors stop processing the form and display the errors and login form if(!empty($error)) { echo '<strong>There were some errors...</strong><br />'; echo $error . '<br />'; include("inc/forms/login.form.php"); } // If there is no $error continue the process else { $password = md5($password); // md5 the passord $user_query = $db->query("SELECT * FROM member WHERE username = '$username' AND password = '$password' LIMIT 1"); // SQL Select to get the member if($db->num_rows($user_query) == 1) // make sure there was a match, and only one match { $row = $db->fetch_assoc($user_query); if($row['level_id'] == "4") // if the account is not verified stop processing and display an error message { echo '<strong>You need to verify your account</strong>'; echo '<meta http-equiv=Refresh content=2;url=index.php>'; exit(); } // set up the session stuff $_SESSION['logged'] = true; $_SESSION['member_id'] = $row['member_id']; $_SESSION['username'] = $row['username']; $_SESSION['level_id'] = $row['level_id']; $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; // set a few vars ready to log the user in the database $sessionid = session_id(); $ip = $_SERVER['REMOTE_ADDR']; $member_id = $row['member_id']; $username = $row['username']; $date = time(); // update the database and log the user in $log_user_in = $db->query("UPDATE member SET lastlogin = '$date', ip = '$ip', sessionid = '$sessionid' WHERE member_id = '$member_id' AND username = '$username' LIMIT 1 ;"); // if all went well display a success message and redirect to the home page if($log_user_in) { echo '<strong>you are now logged in</strong>'; echo '<meta http-equiv=Refresh content=2;url=index.php>'; } // if something went wrong with the login display an error and the login form else { echo '<strong>There was an error, please try again</strong><br /><br />'; include("inc/forms/login.form.php"); } } // if the username or password are incorrect display an error message and the login form else { echo '<strong>Username or Password incorrect</strong><br /><br />'; include("inc/forms/login.form.php"); } } } else // if the form has not been submitted display the login form { include("inc/forms/login.form.php"); } } Link to comment https://forums.phpfreaks.com/topic/179770-classes/ Share on other sites More sharing options...
Mchl Posted October 31, 2009 Share Posted October 31, 2009 Sure class userFunctions{ public function login() { //put the rest of your code here } } Done. ... Well, I guess you didn't expect that. In most cases you can't just take a function and 'convert' it to class. That's naive approach to OOP. Classes are used to model some (real or abstract) objects, processes and relations between them, and understanding how to do it properly may take quite a lot time. See the tutorials on OOP we have on PHPfreaks. You might start here: http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect Link to comment https://forums.phpfreaks.com/topic/179770-classes/#findComment-948460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.