poetichotline Posted December 23, 2008 Share Posted December 23, 2008 Here is the code for my signin part of my website <? include('layout.inc'); ?> <div style="position:absolute; top:110; left:252; width:500; height:300; z-index:1;"> <?php //session_start(); ob_start(); $username = $_POST['username']; $password = $_POST['password']; $errors[] = array(); if(!$username){ $errors[] = "You did not supply a username!"; } if(!$username){ $errors[] = "You did not supply a password!"; } if(count($errors) >= 1){ echo "The following error(s) occured:<br>\n"; foreach($errors AS $error){ echo $error . "<br>\n"; } }else { $sql = "SELECT * FROM `userinfo` WHERE `username`='".$username."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "The username you supplied does not exist!"; }else { $sql2 = "SELECT * FROM `userinfo` WHERE `username`='".$username."' AND `password`='".md5($password)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) == 0){ echo "Username and password combination incorrect!"; }else { $row = mysql_fetch_assoc($res2); // we're going to set the user id // for cookies setcookie('id',$row['uid'],time()+86400); // for sessions $_SESSION['uid'] = $row['id']; header("Location: my_account.php"); } } } ob_end_flush(); ?> When I run it, I get a very unusual error. It simply displays error: message array and that's it. Any ideas what is not working? Thanks in advance for all your help. Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/ Share on other sites More sharing options...
cwarn23 Posted December 23, 2008 Share Posted December 23, 2008 Well one bug I have noticed is that you have output your headers after submitting some browser output. All headers including sending the cookie must be done before any browser output is made. So your script will need totally rearranging so your headers & cookies are sent first then the browser output is sent. Of course you can use Javascript to submit cookies and to redirect after browser output but wouldn't recommend it. Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722090 Share on other sites More sharing options...
poetichotline Posted December 23, 2008 Author Share Posted December 23, 2008 any easy way to do that? The problem I'm having currently is the cookie submission data is only in response to a correct forum filled out... so not sure how to put that first. Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722093 Share on other sites More sharing options...
Andy-H Posted December 23, 2008 Share Posted December 23, 2008 if(!$username){ $errors[] = "You did not supply a username!"; } if(!$password){ // was if !$username $errors[] = "You did not supply a password!"; } Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722097 Share on other sites More sharing options...
poetichotline Posted December 23, 2008 Author Share Posted December 23, 2008 if(!$username){ $errors[] = "You did not supply a username!"; } if(!$password){ // was if !$username $errors[] = "You did not supply a password!"; } That isn't what's giving the error. I think it does have to do with the headers not being executed in the begining... but I'm not sure how to fix that and still keep the forum working. Any ideas? or would you recommend creating an entirely different login script with different syntax? Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722101 Share on other sites More sharing options...
cwarn23 Posted December 23, 2008 Share Posted December 23, 2008 To give you an idea of what the code should look like I have debuged the headers and hopefully there shouldn't be that many bugs left in the script below: <?php session_start(); ob_start(); $username = $_POST['username']; $password = $_POST['password']; $errors[] = array(); if(!$username){ $errors[] = "You did not supply a username!"; } if(!$password){ $errors[] = "You did not supply a password!"; } if(count($errors) >= 1){ }else { $sql = "SELECT * FROM `userinfo` WHERE `username`='".$username."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ }else { $sql2 = "SELECT * FROM `userinfo` WHERE `username`='".$username."' AND `password`='".md5($password)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) == 0){ }else { $row = mysql_fetch_assoc($res2); // we're going to set the user id // for cookies setcookie('id',$row['uid'],time()+86400); // for sessions $_SESSION['uid'] = $row['id']; header("Location: my_account.php"); } } } //========== if(count($errors) >= 1){ echo "The following error(s) occured:<br>\n"; foreach($errors AS $error){ echo $error . "<br>\n"; } }else { if(mysql_num_rows($res) == 0){ echo "The username you supplied does not exist!"; }else { if(mysql_num_rows($res2) == 0){ echo "Username and password combination incorrect!"; } } } ob_end_flush(); include('layout.inc'); ?> <div style="position:absolute; top:110; left:252; width:500; height:300; z-index:1;"> Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722139 Share on other sites More sharing options...
poetichotline Posted December 23, 2008 Author Share Posted December 23, 2008 I'm still getting the strange error message of Array. It simply says Sorry. there was the following errors: Array. Note: thats not a mysql error. It's the error message generated by the code. Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722797 Share on other sites More sharing options...
cwarn23 Posted December 23, 2008 Share Posted December 23, 2008 I have tested the script and try removing the below line of code: $errors[] = array(); It seems to append to the array instead of defining it. So the entire code should look like the below: <?php session_start(); ob_start(); $username = $_POST['username']; $password = $_POST['password']; if(!$username){ $errors[] = "You did not supply a username!"; } if(!$password){ $errors[] = "You did not supply a password!"; } if(count($errors) >= 1){ }else { $sql = "SELECT * FROM `userinfo` WHERE `username`='".$username."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ }else { $sql2 = "SELECT * FROM `userinfo` WHERE `username`='".$username."' AND `password`='".md5($password)."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) == 0){ }else { $row = mysql_fetch_assoc($res2); // we're going to set the user id // for cookies setcookie('id',$row['uid'],time()+86400); // for sessions $_SESSION['uid'] = $row['id']; header("Location: my_account.php"); } } } //========== if(count($errors) >= 1){ echo "The following error(s) occured:<br>\n"; foreach($errors AS $error){ echo $error . "<br>\n"; } }else { if(mysql_num_rows($res) == 0){ echo "The username you supplied does not exist!"; }else { if(mysql_num_rows($res2) == 0){ echo "Username and password combination incorrect!"; } } } ob_end_flush(); include('layout.inc'); ?> <div style="position:absolute; top:110; left:252; width:500; height:300; z-index:1;"> Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722822 Share on other sites More sharing options...
poetichotline Posted December 24, 2008 Author Share Posted December 24, 2008 thank you. I actually just took out the errors and did it manually (for each error check I simply changed it to echo) It works now! thanks For knowledge sake... do you know why it appended to the array instead of defining it? Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722872 Share on other sites More sharing options...
cwarn23 Posted December 24, 2008 Share Posted December 24, 2008 thank you. I actually just took out the errors and did it manually (for each error check I simply changed it to echo) It works now! thanks For knowledge sake... do you know why it appended to the array instead of defining it? I did a few tests and disovered that it was because you were defining a 2 dimensional array. An example of a 2 dimensional array is $var[id][12]; $errors[]=array(); In your script was the above code which defined a two dimensional array. However, if you remove the brackets [] after the $errors then it will make it a regular 1 dimensional array. The script for defining the 1 dimensional array is as follows: $errors=array(); By defining a 1 dimensional array, it doesn't get your foreach loop mixed up with the extra dimension and due to the second dimension, your script just said that the two dimensional array was an array as it couldn't read the value of that array. Hope that helps. Link to comment https://forums.phpfreaks.com/topic/138138-sign-in-forum-trouble/#findComment-722937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.