makka Posted April 22, 2007 Share Posted April 22, 2007 hello im using extract($_POST); to save me time getting them but i want to know how it saves them if in my html forum its like input name="username" input name="password" would that then be turned into $username and $password ? also i would like to know how to make a error list so something like if ($username == "") { $errors++ $errorlist .= "Username field is required.<br />"; } would that work like add that error on to the end of the string so if there were more than one error it would just add them on eg if ($username == "") { $errors++ $errorlist .= "Username field is required.<br />"; } if ($password== "") { $errors++ $errorlist .= "password is required.<br />"; } if ($errors > 0) { die("sorry there were the following errors <br /> $errorlist") } lets say the user didn't enter anything in both of those fields the errorlist would be Username field is required. password is required. would that work or would it just echo out password is required. not the other errors? also how can i check if there is some not allowed chars in a password or a username say the letters "o /" was not allowed how can i check through and see if there are in if they are ad another error on also one last thing while im at it what about seeing how long the user input is that is a loop correct? thanks for your time... Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 hello im using extract($_POST); to save me time getting them but i want to know how it saves them if in my html forum its like input name="username" input name="password" would that then be turned into $username and $password ? Yes, but it's a very insecure method. (Read up on "SQL injection") As for the errorlist, Initialise variables first. $errors = 0; $errorlist = ''; And you need ; after $errors++ if ($username == "") { $errors++; $errorlist .= "Username field is required.<br />"; } Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 thanks very much could you tell me another better way of doing it?? p.s what about "also how can i check if there is some not allowed chars in a password or a username say the letters "o /" was not allowed how can i check through and see if there are in if they are ad another error on also one last thing while im at it what about seeing how long the user input is that is a loop correct?" Quote Link to comment Share on other sites More sharing options...
unkwntech Posted April 22, 2007 Share Posted April 22, 2007 You could check for illeagle characters/phrases with preg_replace. $pattern = "/"; $replacement = ""; $newUsername = preg_replace($pattern, $replacement, $username); if($username != $newUsername) { $errors++; $errorlist .= "Username Contains invalid characters!.<br />"; } preg_replace will check the $username variable for a "/" if it finds any it will remove it and the new string will be set as the variable $newUsername so if you compare $username and $newUsername you will find that they are not the same so you will know that $username contained a character that is not valid. I am sure that there is a better way to do this I just don't know how. This is a verry crude method. Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 preg_match("/", $username)==1 might work thanks Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 22, 2007 Share Posted April 22, 2007 i like to put my errors into an array and run through them with a foreach loop. as for $_POST, just build a sanitize function and run all the $_POST variables through it, and keep using $_POST['fieldname'] as your variable name. no need to rename variables. Quote Link to comment Share on other sites More sharing options...
Glyde Posted April 22, 2007 Share Posted April 22, 2007 i like to put my errors into an array and run through them with a foreach loop. as for $_POST, just build a sanitize function and run all the $_POST variables through it, and keep using $_POST['fieldname'] as your variable name. no need to rename variables. I do the same, only I run a simple implode with my errors. I usually have my errors assigned to $errorList or something, and do: <?php if (count($errorList)) { $output = "<ul><li>" . implode("</li><li>", $errorList) . "</li></ul>"; } ?> Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 could i have some examples please im not so good with just words ohh and is this ok?? if (preg_match("/[^A-z0-9_\-]/", $username)==1) { $errors++; $errorlist .= "Username must be alphanumeric and contain no spaces.<br />"; } is this any good for looking at emails? if (! is_email($email1)) { $errors++; $errorlist .= "Email isn't valid.<br />"; } Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 This will have the same effect as extract($_POST) except the data will be cleansed. <?php function sanitize ($item) { $item = get_magic_quotes_gpc() ? stripslashes($item) : $item; return mysql_real_escape_string($item); } foreach ($_POST as $name => $value) { $$name = sanitize ($value); } ?> Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 thanks just a few more things i need to know now.. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 22, 2007 Share Posted April 22, 2007 I do the same, only I run a simple implode with my errors. I usually have my errors assigned to $errorList or something, and do: <?php if (count($errorList)) { $output = "<ul><li>" . implode("</li><li>", $errorList) . "</li></ul>"; } ?> let me ask, when you write: if(count($errorList)){ 1. is the true/false value of that statement because count($errorList) would amount to zero, therefore be false? and if it's anything but zero, it's true? 2. what would the difference be if it was if(isset($errorList)){? what would return true and what would return false there? i think i might adopt the bulleted list error display. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 1. Yes 2. if $errorList has been initialized with "$errorlist = ''; " then isset() will always return true. You could check for empty($errorlist); Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 22, 2007 Share Posted April 22, 2007 1. Yes 2. if $errorList has been initialized with "$errorlist = ''; " then isset() will always return true. You could check for empty($errorlist); thanks for clearing that up, barand. sorry for the quick hijack. Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 hehe your welcome i oculd do with fast help like that Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 oh and is there any tips you can give to me for making a mmropg Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 Have a look in the contest forum. The last contest was for such a game. Quote Link to comment Share on other sites More sharing options...
makka Posted April 22, 2007 Author Share Posted April 22, 2007 yeh i had a look at those thanks ohh i do have one other question i need asap how can i compare a string to something and if it dosent match it will ad something to the string untill it dose so like a then b then c .. so on then it would be like aa ab ac ect including numbers Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 23, 2007 Share Posted April 23, 2007 how can i compare a string to something and if it dosent match it will ad something to the string untill it dose so like a then b then c .. so on then it would be like aa ab ac ect including numbers for future reference, start a new thread. <?php $array = array('a', 'b', 'c', 'd', 'e', 'f', 'g'); if($something != $somethingElse){ foreach($array as $val){ $something .= $val; } } ?> Quote Link to comment 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.