Jump to content

HartMan

New Members
  • Posts

    2
  • Joined

  • Last visited

HartMan's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. sorry it took so long to respond ive changed my code a bit but now when i pass the username and password i get back this error "Trying to get property of non-object in C:\wamp\www\whatscookincatering\lib\functions.php on line 23" here is what my code looks like now index.php <?php require '../blog.php'; $data = array(); session_start(); # check required fields if( empty( $_POST['username'] ) || empty( $_POST['password'] ) ){ $data['msg'] = 'Please fill out both fields to log in.'; }else{ $DB = new mysqli( 'localhost', $config['dbusername'], $config['dbpassword'], $config['database'] ); # check if login is correct: $success = check_login( $_POST['username'],$_POST['password'],$DB ); # assign appropriate message: $data['msg'] = $success? 'Thank you for logging in': // success 'Wrong username or password.'; // failure } view('../admin/login', $data); ?> functions.php <?php function view($path, $data = null) { if ( $data ){ extract($data); } $path = 'views/' . $path . '.tmpl.php'; include "../views/layout.php"; } function check_login( $username,$password,mysqli $DB ) { $query = $DB->prepare( "SELECT 1 FROM users WHERE username=? AND password=?" ); $password = md5( $password ); # bind the submitted username/password to the statement $query->bind_param( 'ss',$username,$password ); # query the DB and check number of rows returned to determine success $result = $query->execute(); return ($result->num_rows === 1)? true: false; } ?> this is line 23 "return ($result->num_rows === 1)?"
  2. hello all, first post here. im fairly new to php and am still trying to really get a hold of what im doing. right now im just trying to build a simple login function for my site and am completely stuck. here is what i have so far. in function authuser im trying to create a query, return the result, compare it with those that were posted on index.php and if it matches the database i would like the login function. to start the session. i hope that makes sense. and if there is a better way to do this or something im missing please let me know index.php if($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_POST['username']; $password = md5($_POST['password']); if(empty($username) || empty($password)){ $data['status'] = 'Please fill out both inputs'; } else { // login authuser($username,$password); } } functions.php function login($username,$password) { session_start(); } function authuser($username,$password) { $sql = "SELECT * FROM users WHERE username='$username' and password='$password'"; $results = mysql_query($sql); $rows = mysql_num_rows($results); if($rows==1) { session_register("admin"); } else { echo "Wrong Username or Password"; } }
×
×
  • 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.