graham23s Posted February 17, 2008 Share Posted February 17, 2008 Hi Guys, in my login code im implementing some error checking i have initialsed an array for the errors but when trying to display them i get an "Invalid argument supplied for foreach() " <?php session_start(); if(isset($_POST['submit'])) { include("inc/dbconnection.php"); // vars // $email = mysql_real_escape_string(trim($_POST['email'])); $password = mysql_real_escape_string(trim($_POST['password'])); $cookie = mysql_real_escape_string($_POST['remember']); // initialise errors array // $errors = array(); // check for empty fields use javascript to // if(empty($email) || empty($password)) { $errors = "<br /><div class=\"formerror2\"><img src=\"images/cross.gif\"> <b>You never filled in both login fields.</b></div>"; } // see if the details are in the database // $querylogin = "SELECT `id`,`email`,`password`,`first_name` FROM `fcp_customers` WHERE `email`='$email' AND `password`='$password' LIMIT 1"; $resultslogin = mysql_query($querylogin) or die (mysql_error()); $row = mysql_fetch_array($resultslogin); // results from the login attempt // $num = mysql_num_rows($resultslogin); if($num != 1) { include("inc/header.php"); include("inc/navigation.php"); // display errors // foreach($errors as $error) { print("test$error"); } include("inc/footer.php"); exit; } //else { // there was results so update the login timer and set a session // $querytimer = mysql_query("UPDATE `fcp_customers` SET `lastloggedin`=now() WHERE `email`='$email' AND `password`='$password'"); $_SESSION['id'] = $row['id']; $_SESSION['username'] = $row['username']; $_SESSION['first_name'] = $row['first_name']; $_SESSION['logged_in'] = 'yes'; $cookie_id = $row['id']; $cookie_customername = $row['username']; // check if the remember is set value of remember is yes // if(isset($cookie)) { // if it is set the set a cookie // setcookie("customers_cookie_id", $cookie_id, time()+3600); setcookie("customers_cookie_customer", $cookie_customername, time()+3600); } // redirect to members page // header("Location: account.php"); die(); //} } // end isset // ?> <?php // include the database connection // include("inc/dbconnection.php"); include("inc/online.php"); include("inc/header.php"); include("inc/navigation.php"); ?> <?php // standard header // print("<div class=\"subheader\"><div id=\"title\">Home > <span class=\"blue\">Login to your account</span></div>You can update your personal details and view orders in here. forgot your password? no worries recover it <a href=\"recoverpassword.php\">here</a>.</div>"); // check to see if the session exists // if(isset($_SESSION['id'])) { print("<div id=\"shopping_login_error\">You are already logged in.</div>"); include("inc/footer.php"); exit; } // login box // print("<form action=\"login.php\" method=\"post\" name=\"loginform\" onSubmit=\"return logincheck()\">"); print("<table class=\"login_table\" border=\"1\" cellpadding=\"5\">"); print("<tr><td class=\"login_border\" align=\"right\">E-Mail:</td><td class=\"login_border\" align=left><input type=\"text\" size=\"40\" name=\"email\" /></td></tr>"); print("<tr><td class=\"login_border\" align=\"right\">Password:</td><td class=\"login_border\" align=left><input type=\"password\" size=\"40\" name=\"password\" /></td></tr>"); print("<tr><td class=\"login_border\" align=\"right\"> </td><td class=\"login_border\" align=left><input type=\"checkbox\" name=\"remember\" value='yes'>Remember me next time i log in.</td></tr>"); print("<tr><td class=\"login_border\" colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Login\" class=\"btn\"></td></tr>"); print("</table></form>\n"); ?> <?php // include the footer // include("inc/footer.php"); ?> i'm not sure what the problem could be. thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/91570-invalid-argument-on-foreach/ Share on other sites More sharing options...
wildteen88 Posted February 17, 2008 Share Posted February 17, 2008 In the following few lines: // initialise errors array // $errors = array(); // check for empty fields use javascript to // if(empty($email) || empty($password)) { $errors = "<br /><div class=\"formerror2\"><img src=\"images/cross.gif\"> <b>You never filled in both login fields.</b></div>"; } You initiate the $error variable as an array. Then a few lines down you are reseting the $error variable as a string which is why you are getting the error message. You should add [] after $error to add the string to an array. So use the following instead: $errors[] = "<br /><div class=\"formerror2\"><img src=\"images/cross.gif\"> <b>You never filled in both login fields.</b></div>"; Quote Link to comment https://forums.phpfreaks.com/topic/91570-invalid-argument-on-foreach/#findComment-469020 Share on other sites More sharing options...
revraz Posted February 17, 2008 Share Posted February 17, 2008 Also, you are doing the foreach only if rows returned does not = 1. If there are 0 rows but the name/pw is wrong, your array will be empty. Seems like you would want your $error checking outside of the loop where you check for valid accounts. Quote Link to comment https://forums.phpfreaks.com/topic/91570-invalid-argument-on-foreach/#findComment-469021 Share on other sites More sharing options...
spfoonnewb Posted February 17, 2008 Share Posted February 17, 2008 Yeah there are a few problems with that code. <?php session_start(); if(isset($_POST['submit'])) { include("inc/dbconnection.php"); // vars // $email = mysql_real_escape_string(trim($_POST['email'])); $password = mysql_real_escape_string(trim($_POST['password'])); $cookie = mysql_real_escape_string($_POST['remember']); if (!empty($email) && (!empty($password))) { if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { // results from the login attempt // $querylogin = mysql_query("SELECT `id`,`email`,`password`,`first_name` FROM `fcp_customers` WHERE `email`='$email' AND `password`='$password' LIMIT 1") or die(mysql_error()); if (mysql_num_rows($resultslogin) == 1) { // A row was found matching the user // $querytimer = mysql_query("UPDATE `fcp_customers` SET `lastloggedin`=now() WHERE `email`='$email' AND `password`='$password'"); while ($row = mysql_fetch_array($querylogin)) { $_SESSION['id'] = $row['id']; $_SESSION['username'] = $row['username']; $_SESSION['first_name'] = $row['first_name']; $_SESSION['logged_in'] = 'yes'; $cookie_id = $row['id']; $cookie_customername = $row['username']; } // check if the remember is set value of remember is yes // if(isset($cookie)) { // if it is set the set a cookie // setcookie("customers_cookie_id", $cookie_id, time()+3600); setcookie("customers_cookie_customer", $cookie_customername, time()+3600); } // redirect to members page // header("Location: account.php"); exit; } else { return_error("Invalid e-mail and/or password provided."); } } else { return_error("Invalid e-mail address format provided."); } } else { return_error("E-mail and/or password fields were empty."); } } function return_error($text) { include("inc/header.php"); include("inc/navigation.php"); echo "<br /><div class=\"formerror2\"><img src=\"images/cross.gif\"> <b>{$text}</b></div>"; include("inc/footer.php"); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/91570-invalid-argument-on-foreach/#findComment-469039 Share on other sites More sharing options...
graham23s Posted February 17, 2008 Author Share Posted February 17, 2008 thanks guys for that, it gave me another approach to the login page. thanks again Graham Quote Link to comment https://forums.phpfreaks.com/topic/91570-invalid-argument-on-foreach/#findComment-469072 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.