crouzilles Posted June 1, 2009 Share Posted June 1, 2009 Hello, I am pretty new to php and I have the following question. I have a page called signup.php so users on my site can sign up and become members. At the top of the page I have all my php and at the bottom all my html. When a user wants to sign up, he/she is directed to the signup.php page. I do the following to check if it is a post back if ($_SERVER['REQUEST_METHOD'] == 'POST') { If it is not a post back I don't check for request arguments and just show the html, but if it is a post back I check for request arguements as this means the user tried to sign up. My problem is as follow. When a problem occurs, for example the username already exists in the database , I set a variable which I can use in the html part to tell the user what the problem is, but what I want to do is exit the if statement as there is no need for other things to be checked. So what I did was to use the exit call, but this seems to stop the entire script and I get a blank page. Is there a way to just exit an if statement? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/ Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Hello, I am pretty new to php and I have the following question. I have a page called signup.php so users on my site can sign up and become members. At the top of the page I have all my php and at the bottom all my html. When a user wants to sign up, he/she is directed to the signup.php page. I do the following to check if it is a post back if ($_SERVER['REQUEST_METHOD'] == 'POST') { If it is not a post back I don't check for request arguments and just show the html, but if it is a post back I check for request arguements as this means the user tried to sign up. My problem is as follow. When a problem occurs, for example the username already exists in the database , I set a variable which I can use in the html part to tell the user what the problem is, but what I want to do is exit the if statement as there is no need for other things to be checked. So what I did was to use the exit call, but this seems to stop the entire script and I get a blank page. Is there a way to just exit an if statement? Thank you Seems you have structural issue with the script. Please post the script here, so that we can have better understanding about the issue. Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/#findComment-846981 Share on other sites More sharing options...
crouzilles Posted June 1, 2009 Author Share Posted June 1, 2009 This is the script so far. html below body tag not included. <?php # Start the session session_start(); # Source utility functions require_once("utils.php"); # Source db functionality require_once("db.php"); $p_username = ""; $p_email = ""; $p_fname = ""; $p_lname = ""; $p_country = ""; function username_exists($uname) { global $logger, $DB; $logger->log("-->utils.php--username_exists: ".$uname,PEAR_LOG_INFO); $sql = "select username from tcustomer where username = $1"; $result = pg_query_params($DB, $sql, array($uname)); if (!$result) { $logger->log("-->utils.php--username_exists - ".pg_last_error(), PEAR_LOG_ERR); $logger->log("-->utils.php--username_exists - Got unknown result form the DB", PEAR_LOG_WARNING); return -1; } else { # ok, we have successfuly run the query, do we have a row? $logger->log("-->utils.php - Got result from the DB", PEAR_LOG_INFO); $rows = pg_num_rows($result); if($rows == 1) { # ok, we have found the user in the table, this means we can't use it to register a new user $logger->log("-->utils.php - Result has 1 row, return 1", PEAR_LOG_INFO); return 1; } else { return 0; } } } if ($_SERVER['REQUEST_METHOD'] == 'POST') { # Lets check we have values in username and password fields if (!empty($_POST['username']) && !empty($_POST['password_1']) && !empty($_POST['password_2']) && !empty($_POST['email']) && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['country'])) { $logger->log("-->do_signup.php - Getting registration fields", PEAR_LOG_INFO); $p_username = $_POST['username']; $p_email = $_POST['email']; $p_fname = $_POST['fname']; $p_lname = $_POST['lname']; $p_country = $_POST['country']; $pwrd_1 = md5(safe($_POST['password_1'])); $pwrd_2 = md5(safe($_POST['password_2'])); if (strcmp($pwrd_1,$pwrd_2) != 0) { $logger->log("-->do_signup.php - Password fields do not match, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "signup_password_missmatch"; exit; } $uname = safe($_POST['username']); $uname_check = username_exists($uname); db_close(); if ($uname_check == -1) { $logger->log("-->do_signup.php - Error occured while checking username exists, redirecting back to signup.php", PEAR_LOG_ERR); $_SESSION["feedback"] = "db_unknown_result"; exit; } elseif ($uname_check == 1) { $logger->log("-->do_signup.php - Username ".$uname." is already in use, redirecting back to signup.php", PEAR_LOG_INFO); $_SESSION["feedback"] = "username_taken"; exit; } $email = safe($_POST['email']); $fname = safe($_POST['fname']); $lname = safe($_POST['lname']); $country = safe($_POST['country']); $uid = generateRandomString(); $logger->log("-->do_signup.php - About to add user ".$uname." to the database", PEAR_LOG_INFO); $sql = "insert into tcustomer( username, password, email, fname, lname, country, uid) values( $1,$2,$3,$4,$5,$6,$7)"; $result = pg_query_params($DB, $sql, array($uname, $pwrd_1, $email, $fname, $lname, $country, $uid)); if(!$result) { $logger->log(pg_last_error(), PEAR_LOG_ERROR); $logger->log("-->do_signup.php - Got unknown result form the DB, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "db_unknown_result"; db_close(); exit; } else { # ok, we have successfuly run the query, do we have an affected row? $logger->log("-->do_signup.php - Got result from the DB", PEAR_LOG_INFO); $rows = pg_affected_rows($result); if($rows == 1) { # ok, we have successfuly inserted a row. $logger->log("-->do_signup.php - Result has 1 row, all is fine", PEAR_LOG_INFO); send_need_confirmation_mail($email); $_SESSION["feedback"] = "just_signed_up"; exit; } else { # we don't seem to have been able to insert anything for some strange reason $logger->log("-->do_signup.php - Result has not got 1 row, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "db_no_result"; db_close(); exit; } } } else { $logger->log("-->do_signup.php - User did not enter all required fields, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "signup_missing"; db_close(); exit; } } ?> <!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" dir="ltr" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Title goes here</title> <meta name="generator" content="EditPlus" /> <link rel="stylesheet" href="css/freshy.css" type="text/css" media="screen" /> <!--[if gte IE 7]> <link rel="stylesheet" href="css/freshy_ie7.css" type="text/css" media="screen" /> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" href="css/freshy_ie6.css" type="text/css" media="screen" /> <![endif]--> <link rel="stylesheet" href="css/global.css" type="text/css" media="screen" /> <script type="text/javascript" src="utils.js"></script> </head> <body> Basically, anything below the body tag does not show if there is an error and I think this is because I call exit. Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/#findComment-846988 Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Try this: <?php # Start the session session_start(); # Source utility functions require_once("utils.php"); # Source db functionality require_once("db.php"); $p_username = ""; $p_email = ""; $p_fname = ""; $p_lname = ""; $p_country = ""; function username_exists($uname) { global $logger, $DB; $logger->log("-->utils.php--username_exists: ".$uname,PEAR_LOG_INFO); $sql = "select username from tcustomer where username = $1"; $result = pg_query_params($DB, $sql, array($uname)); if (!$result) { $logger->log("-->utils.php--username_exists - ".pg_last_error(), PEAR_LOG_ERR); $logger->log("-->utils.php--username_exists - Got unknown result form the DB", PEAR_LOG_WARNING); return -1; } else { # ok, we have successfuly run the query, do we have a row? $logger->log("-->utils.php - Got result from the DB", PEAR_LOG_INFO); $rows = pg_num_rows($result); if($rows == 1) { # ok, we have found the user in the table, this means we can't use it to register a new user $logger->log("-->utils.php - Result has 1 row, return 1", PEAR_LOG_INFO); return 1; } else { return 0; } } } if ($_SERVER['REQUEST_METHOD'] == 'POST') { # Lets check we have values in username and password fields if (!empty($_POST['username']) && !empty($_POST['password_1']) && !empty($_POST['password_2']) && !empty($_POST['email']) && !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['country'])) { $logger->log("-->do_signup.php - Getting registration fields", PEAR_LOG_INFO); $p_username = $_POST['username']; $p_email = $_POST['email']; $p_fname = $_POST['fname']; $p_lname = $_POST['lname']; $p_country = $_POST['country']; $pwrd_1 = md5(safe($_POST['password_1'])); $pwrd_2 = md5(safe($_POST['password_2'])); if (strcmp($pwrd_1,$pwrd_2) != 0) { $logger->log("-->do_signup.php - Password fields do not match, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "signup_password_missmatch"; exit; } $uname = safe($_POST['username']); $uname_check = username_exists($uname); db_close(); if ($uname_check == 1) { $logger->log("-->do_signup.php - Username ".$uname." is already in use, redirecting back to signup.php", PEAR_LOG_INFO); $_SESSION["feedback"] = "username_taken"; } else { if ($uname_check == -1) { $logger->log("-->do_signup.php - Error occured while checking username exists, redirecting back to signup.php", PEAR_LOG_ERR); $_SESSION["feedback"] = "db_unknown_result"; exit; } $email = safe($_POST['email']); $fname = safe($_POST['fname']); $lname = safe($_POST['lname']); $country = safe($_POST['country']); $uid = generateRandomString(); $logger->log("-->do_signup.php - About to add user ".$uname." to the database", PEAR_LOG_INFO); $sql = "insert into tcustomer( username, password, email, fname, lname, country, uid) values( $1,$2,$3,$4,$5,$6,$7)"; $result = pg_query_params($DB, $sql, array($uname, $pwrd_1, $email, $fname, $lname, $country, $uid)); if(!$result) { $logger->log(pg_last_error(), PEAR_LOG_ERROR); $logger->log("-->do_signup.php - Got unknown result form the DB, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "db_unknown_result"; db_close(); exit; } else { # ok, we have successfuly run the query, do we have an affected row? $logger->log("-->do_signup.php - Got result from the DB", PEAR_LOG_INFO); $rows = pg_affected_rows($result); if($rows == 1) { # ok, we have successfuly inserted a row. $logger->log("-->do_signup.php - Result has 1 row, all is fine", PEAR_LOG_INFO); send_need_confirmation_mail($email); $_SESSION["feedback"] = "just_signed_up"; exit; } else { # we don't seem to have been able to insert anything for some strange reason $logger->log("-->do_signup.php - Result has not got 1 row, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "db_no_result"; db_close(); exit; } } } } else { $logger->log("-->do_signup.php - User did not enter all required fields, redirecting to signup.php", PEAR_LOG_WARNING); $_SESSION["feedback"] = "signup_missing"; db_close(); exit; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/#findComment-846997 Share on other sites More sharing options...
laffin Posted June 1, 2009 Share Posted June 1, 2009 U can only break out early from functions (return) and loops (for/while) so either convert to function as anupam has done or use a do/while fake loop $name='laffin'; $pass=''; do { if(empty($name)) { $error='Name required'; break; } if(empty($password)) { $error='password required'; break; } } while(FALSE); if(isset($error)) echo 'We got an error'; else echo 'Success'; Since the expression in WHILE is always FALSE, this will make 1 pass loop, with break statements to exit the loop early Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/#findComment-847003 Share on other sites More sharing options...
crouzilles Posted June 1, 2009 Author Share Posted June 1, 2009 Thank you for the help, very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/160497-solved-how-to-stop-script-and-display-html/#findComment-847009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.