Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Snippet #1 */
- <?php
- // login_check.php
- define("server", "your_server");
- define("user", "your_name");
- define("password", "your_pass");
- define("name", "your_dbname");
- var $connection;
- $this->connection = mysql_connect(server,user,pass) or die(mysql_error());
- mysql_select_db(name, $this->connection) or die(mysql_error());
- function is_logged_in () {
- if (!($_SESSION["id"]) || ($_SESSION["id"] == "") || ($_SESSION["id"] == 0)) {
- Header("Location: ./login.php");
- exit();
- }
- }
- function clean_input($input) {
- $clean = array("\\",'<','>','`',':',';','/','(',')','{','}','[',']');
- //$with = array();
- return str_ireplace($clean,'', $input);
- }
- function login_check ($forms) {
- $error = "";
- $username = clean_input($forms["username"]);
- $password = clean_input($forms["password"]);
- if (trim($username) == "") $error .= "<li>Your username is empty.</li>";
- if (trim($password) == "") $error .= "<li>Your password is empty.</li>";
- /* from here, do your sql query to query the database to search for existing record with correct username and password */
- $query = "SELECT password, username FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
- $result = mysql_query($query, $this->connection);
- if(!$result || (mysql_numrows($result) < 1)) {
- $error = "Invalid username or password";
- }else
- {
- $error = "";
- }
- if (trim($error)!="") return $error;
- }
- function login ($forms) {
- $username = clean_input($forms["username"]);
- $password = clean_input($forms["password"]);
- /* do your sql query again, but now returning the id of member */
- $query = "SELECT member_id FROM users WHERE username = '".mysql_real_escape_string($username)."' AND password = '".mysql_real_escape_string($password)."'";
- $result = mysql_query($query, $this->connection);
- $result = mysql_query($query, $this->connection);
- if(!$result || (mysql_numrows($result) < 1)) {
- $id = 0;
- }else
- {
- $id = $result;
- }
- return $id;
- }
- ?>
- /* Snippet #2 */
- <?php
- // login.php
- session_start();
- include ("login_check.php");
- if ($_POST) {
- $error = login_check($_POST);
- if (trim($error)=="") {
- $_SESSION["id"] = login($_POST);
- Header("Location: ./index.php") /* Redirect validated member */
- exit();
- } else {
- print "Error:$error";
- }
- }
- ?>
- /* Snippet #3 */
- <?php
- // index.php
- include("login_check.php");
- session_start();
- is_logged_in();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment