xgd Posted July 10, 2009 Share Posted July 10, 2009 Hello, I made this login script from some book that uses cookies to log a registered user in. It consists of login.php, and 2 include files. It works, but the thing is i wanna rewrite it so that it is all one file (login.php) WITHOUT using custom functions in order to understand it better. I have tried to rewrite it on my own but failed and cannot figure it out. can someone help out? Here are the working scripts: login_functions.inc.php <?php function check_login($dbc, $email = '', $pass = '') { if (empty($email)) { $errors[] = "you didnt enter EMAIL<br />"; } else { $e = trim($email); } if (empty($pass)) { $errors[] = "Didnt enter PASS<br />"; } else { $p = trim($pass); } if (empty($errors)) { $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r); return array(true, $row); } else { echo "Youre not in the DB"; } } return array(false, $errors); } ?> login_page.inc.php <?php if (!empty($errors)) { echo "The following errors occured<br />"; foreach ($errors as $msg) { echo $msg."<br />"; } } ?> <h1>Login</h1> <form action="login.php" method="post"> <p>Email address <input type="text" name="email" /></p> <p>Password <input type="text" name="pass" /></p> <p><input type="submit" name="submit" /></p> </form> and login.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>Untitled Document</title> </head> <body> <?php if (@$_POST['submit']) { require_once('includes/login_functions.inc.php'); require_once('db_connect.php'); list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']); if ($check) { setcookie('user_id', $data['user_id']); setcookie('first_name', $data['first_name']); header("Location: index.php"); exit(); } else { $errors = $data; } } include('includes/login_page.inc.php'); ?> </body> </html> All of that works, but i would like to have it all in one file without custom funcstions as i have already said, so i tried to write it as: <!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>Untitled Document</title> </head> <body> <?php if ($_POST['submit']) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter email"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter pass"; } else { $p = trim($_POST['pass']); } if (@$e && @$p) { $q = "SELECT user_id, first_name FROM users WHERE email='$e' ANd pass=SHA1('$e')"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r); } else { echo "youre not in the db"; } } }//end of if post submit //How do i rewrite the following ? $data = ($dbc, $_POST['email'], $_POST['pass']); if ($data) { setcookie('user_id', $data['user_id']); setcookie('first_name', $data['first_name']); header("Location: index.php"); exit(); } ?> <h1>Login</h1> <form action="login.php" method="post"> <p>Email address <input type="text" name="email" /></p> <p>Password <input type="text" name="pass" /></p> <p><input type="submit" name="submit" /></p> </form> ?> </body> </html> But i messed up on line 49 (which i knew would happen :'( ) So can someone help me with this one ? Thanks Link to comment https://forums.phpfreaks.com/topic/165530-solved-cant-rewrite-a-working-script/ Share on other sites More sharing options...
radi8 Posted July 10, 2009 Share Posted July 10, 2009 <!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>Untitled Document</title> </head> <body> <?php if ($_POST['submit']) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter email"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter pass"; } else { $p = trim($_POST['pass']); } if (@$e && @$p) { $q = "SELECT user_id, first_name FROM users WHERE email='$e' ANd pass=SHA1('$e')"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r); } else { echo "youre not in the db"; } } }//end of if post submit if (empty($email)) { $errors[] = "you didnt enter EMAIL<br />"; } else { $e = trim($email); } if (empty($pass)) { $errors[] = "Didnt enter PASS<br />"; } else { $p = trim($pass); } if (empty($errors)) { $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { $row = mysqli_fetch_array($r); $data=array(true, $row); } else { echo "Youre not in the DB"; $data= array(false, $errors); } } if ($data) { setcookie('user_id', $data['user_id']); setcookie('first_name', $data['first_name']); header("Location: index.php"); exit(); } ?> <h1>Login</h1> <form action="login.php" method="post"> <p>Email address <input type="text" name="email" /></p> <p>Password <input type="text" name="pass" /></p> <p><input type="submit" name="submit" /></p> </form> ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/165530-solved-cant-rewrite-a-working-script/#findComment-873100 Share on other sites More sharing options...
xgd Posted July 10, 2009 Author Share Posted July 10, 2009 This works man thanks a lot! Link to comment https://forums.phpfreaks.com/topic/165530-solved-cant-rewrite-a-working-script/#findComment-873129 Share on other sites More sharing options...
xgd Posted July 10, 2009 Author Share Posted July 10, 2009 i will need to check something else on my own and then i will mark the topic as solved, thanks for letting me know. As for code tags, i believe i used the right ones, if i did not then do correct me. Link to comment https://forums.phpfreaks.com/topic/165530-solved-cant-rewrite-a-working-script/#findComment-873142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.