Jump to content

extract($_POST); and one other question


makka

Recommended Posts

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...

Link to comment
Share on other sites

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 />"; }

Link to comment
Share on other sites

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?"

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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 />"; }

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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; }
        }       
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.