zeeshan Posted December 14, 2009 Share Posted December 14, 2009 I have remade few coded just by editing them. Actually the thing is they are not redirecting after the registration. I want the registration page redirect me to a thankyou.php page and then from there to login.php page. I'm unable to do this.The thing is I'm being registered but when i try to login it shows the particular e-mail address wasn't found please check this code. register.php <?phprequire_once 'db.php';$page_mode = isset($_POST['page_mode']) ? $_POST['page_mode'] : '';$error_string = '';if ($page_mode == 'register'){ $email = trim($_POST['email']); // trim to remove whitespace $name = trim($_POST['name']); // trim to remove whitespace $password = $_POST['password']; $conf_password = $_POST['conf_password']; if (!isValidEmail($email)) $error_string .= 'Please enter a valid email address.<br>'; if ($name == '') $error_string .= 'Please enter your name.<br>'; if (strlen(trim($password)) < 6) $error_string .= 'You must enter a password of at least 6 characters.<br>'; if ($password != $conf_password) $error_string .= 'The password and confirmation password do not match.<br>'; if ($error_string == '') { $result = db_query("SELECT id FROM users WHERE email='" . mysql_real_escape_string($email) . "'"); if (mysql_num_rows($result) > 0) $error_string .= 'That email address is already registerd.<br>'; else { $email = mysql_real_escape_string($email); // protect against SQL attacks $name = mysql_real_escape_string($name); $password = sha1($password); // hash password db_query("INSERT INTO users (email, name, password) VALUES ('{$email}', '{$name}', '{$password}')");echo "Hello World"; exit(); } }}function isValidEmail($email = ''){ return preg_match("/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix",$email);}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Register</title> <style type="text/css"> .error_text { color: #FF0000; width: 400px; text-align: center; } .left_box { float: left; width: 150px; text-align: right; padding-right: 5px; } .right_box { clear: right; } </style> </head> <body> <div class="error_text"><?php echo $error_string; ?></div> <form action="register.php" method="post"> <input type="hidden" name="page_mode" value="register"> <div class="left_box">Email address</div> <div class="right_box"><input type="text" name="email" size="30" maxlength="255" value="<?php if (isset($email)) echo $email; ?>"></div> <div class="left_box">Name</div> <div class="right_box"><input type="text" name="name" size="30" maxlength="255" value="<?php if (isset($name)) echo $name; ?>"></div> <div class="left_box">Password</div> <div class="right_box"><input type="password" name="password" size="30"></div> <div class="left_box">Confirm Password</div> <div class="right_box"><input type="password" name="conf_password" size="30"></div> <div class="left_box"> </div> <div class="right_box"><input type="submit" value="Register" size="30"></div> </form> </body></html> thankyou.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Thank You</title> </head> <body> Thank you for registering, click <a href="login.php">here</a> to log in. </body></html> login.php <?phpsession_start();require_once 'db.php';$page_mode = isset($_POST['page_mode']) ? $_POST['page_mode'] : '';$error_string = '';if ($page_mode == 'login'){ $email = $_POST['email']; $password = $_POST['password']; if (trim($email) == '' || trim($password) == '') $error_string .= 'Please enter your email address and password.<br>'; else { $result = db_query("SELECT id, name, password FROM users WHERE email='" . mysql_real_escape_string($email) . "'"); if (!($row = mysql_fetch_assoc($result))) $error_string .= 'The email address was not found.<br>'; elseif ($row['password'] != sha1($password)) $error_string .= 'The password did not match.<br>'; else { $_SESSION['user_id'] = $row['id']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; header('Location: index.php'); exit(); } }}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Register</title> <style type="text/css"> .error_text { color: #FF0000; width: 400px; text-align: center; } .left_box { float: left; width: 150px; text-align: right; padding-right: 5px; } .right_box { clear: right; } </style> </head> <body> <div class="error_text"><?php echo $error_string; ?></div> <form action="login.php" method="post"> <input type="hidden" name="page_mode" value="login"> <div class="left_box">Email address</div> <div class="right_box"><input type="text" name="email" size="30" maxlength="255" value="<?php if (isset($email)) echo $email; ?>"></div> <div class="left_box">Password</div> <div class="right_box"><input type="password" name="password" size="30"></div> <div class="left_box"> </div> <div class="right_box"><input type="submit" value="Log In" size="30"></div> </form> </body></html> Link to comment Share on other sites More sharing options...
Redroest Posted December 14, 2009 Share Posted December 14, 2009 You can simply troubleshoot this one by searching for the error message in your script: i try to login it shows the particular e-mail address wasn't foundreferences to:<?phpif (!($row = mysql_fetch_assoc($result))) $error_string .= 'The email address was not found.<br>';?> Above is the problemscript. (I think) It contains the email not found error so first try to find out if the email is posted by doing the following: <?phpif ($page_mode == 'login'){ $email = $_POST['email']; $password = $_POST['password']; //Test if email has been send echo "<p>".$email."</p>"; if (trim($email) == '' || trim($password) == '') $error_string .= 'Please enter your email address and password.<br>'; else { $result = db_query("SELECT id, name, password FROM users WHERE email='" . mysql_real_escape_string($email) . "'"); if (!($row = mysql_fetch_assoc($result))) $error_string .= 'The email address was not found.<br>'; elseif ($row['password'] != sha1($password)) $error_string .= 'The password did not match.<br>'; else { $_SESSION['user_id'] = $row['id']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; header('Location: index.php'); exit(); } }}?> The above will show an emailadres after someone posts it in the form. When the given email doesn't show up, you know that the error is in the form. When it does show up, something is wrong with your SELECT query but since I cannot discover any errors in your query, I think the problem lies with your form input Link to comment Share on other sites More sharing options...
zeeshan Posted December 14, 2009 Author Share Posted December 14, 2009 Where should i add this? Link to comment Share on other sites More sharing options...
Redroest Posted December 14, 2009 Share Posted December 14, 2009 Just save the code below as your login.php <?phpsession_start();require_once 'db.php';$page_mode = isset($_POST['page_mode']) ? $_POST['page_mode'] : '';$error_string = '';if ($page_mode == 'login'){ $email = $_POST['email']; $password = $_POST['password'];//Test if email has been send echo "<p>".$email."</p>"; if (trim($email) == '' || trim($password) == '') $error_string .= 'Please enter your email address and password.<br>'; else { $result = db_query("SELECT id, name, password FROM users WHERE email='" . mysql_real_escape_string($email) . "'"); if (!($row = mysql_fetch_assoc($result))) $error_string .= 'The email address was not found.<br>'; elseif ($row['password'] != sha1($password)) $error_string .= 'The password did not match.<br>'; else { $_SESSION['user_id'] = $row['id']; $_SESSION['user_name'] = $row['name']; $_SESSION['user_email'] = $row['email']; header('Location: index.php'); exit(); } }}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <title>Register</title> <style type="text/css"> .error_text { color: #FF0000; width: 400px; text-align: center; } .left_box { float: left; width: 150px; text-align: right; padding-right: 5px; } .right_box { clear: right; } </style> </head> <body> <div class="error_text"><?php echo $error_string; ?></div> <form action="login.php" method="post"> <input type="hidden" name="page_mode" value="login"> <div class="left_box">Email address</div> <div class="right_box"><input type="text" name="email" size="30" maxlength="255" value="<?php if (isset($email)) echo $email; ?>"></div> <div class="left_box">Password</div> <div class="right_box"><input type="password" name="password" size="30"></div> <div class="left_box"> </div> <div class="right_box"><input type="submit" value="Log In" size="30"></div> </form> </body></html> Link to comment Share on other sites More sharing options...
zeeshan Posted December 15, 2009 Author Share Posted December 15, 2009 ok thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 15, 2009 Share Posted December 15, 2009 Are you sure you registered that email address? You should be able to use something like phpMyAdmin to browse the users table and make sure that email address is in there. It should only give that error if the email address was not found in the users table. Link to comment Share on other sites More sharing options...
zeeshan Posted December 17, 2009 Author Share Posted December 17, 2009 I have. But i will check it again. Link to comment Share on other sites More sharing options...
zeeshan Posted December 18, 2009 Author Share Posted December 18, 2009 The registration form doesn't store it. I want some help. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2009 Share Posted December 18, 2009 What happens when you use the registration form? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.