Jump to content

Recommended Posts

Sorry for triple post. I involved my code a little bit: added e-mail filter using a flag + a little "noob trick" which replace every "big spaces" by normal ones.

 

$drap = 0;

if (empty ($_POST['pseudo'])) {

echo "Certains champs n'ont pas été remplis!<br>Veuillez <a href='formulaireinsc.php'>réessayer</a>.";

}

elseif (empty($_POST['mdp'])) {

echo "Certains champs n'ont pas été remplis!<br>Veuillez <a href='formulaireinsc.php'>réessayer</a>.";

}

elseif (empty($_POST['email'])) {

echo "Certains champs n'ont pas été remplis!<br>Veuillez <a href='formulaireinsc.php'>réessayer</a>.";

}

elseif (filter_var(($_POST['email']), FILTER_VALIDATE_EMAIL)) {

$drap = 1;

mysql_select_db("apprentissage", $con);

$pseudo = mysql_real_escape_string($_POST['pseudo']);

$mdp = mysql_real_escape_string($_POST['mdp']);

$email = mysql_real_escape_string($_POST['email']);

 

//retrait des espaces

$pseudo = str_replace('  ' ,' ',$pseudo);

$pseudo = str_replace('  ' ,' ',$pseudo);

$pseudo = str_replace('    ' ,' ',$pseudo);

$pseudo = str_replace('    ' ,' ',$pseudo);

$pseudo = str_replace('      ' ,' ',$pseudo);

$pseudo = str_replace('      ' ,' ',$pseudo);

$pseudo = str_replace('        ' ,' ',$pseudo);

$pseudo = str_replace('        ' ,' ',$pseudo);

$pseudo = str_replace('          ' ,' ',$pseudo);

$pseudo = str_replace('          ' ,' ',$pseudo);

$pseudo = str_replace('            ' ,' ',$pseudo);

$pseudo = str_replace('            ' ,' ',$pseudo);

$pseudo = str_replace('              ' ,' ',$pseudo);

$pseudo = str_replace('              ' ,' ',$pseudo);

$pseudo = str_replace('                ' ,' ',$pseudo);

$pseudo = str_replace('                ' ,' ',$pseudo);

$pseudo = str_replace('                  ' ,' ',$pseudo);

$pseudo = str_replace('                  ' ,' ',$pseudo);

$pseudo = str_replace('                    ' ,' ',$pseudo);

$pseudo = str_replace('                    ' ,' ',$pseudo);

$pseudo = str_replace('                      ' ,' ',$pseudo);

$pseudo = str_replace('                      ' ,' ',$pseudo);

//fin retrait des espaces

 

mysql_query("INSERT INTO membres (pseudo,mdp,email) VALUES ('$pseudo', '$mdp', '$email')");

echo "Vous êtes bien inscrit!";

}

elseif ($drap == 0) {

echo "L'adresse mail est incorrecte!";

}

 

Now 2 problems:

- People can still register with the same pseudo than the others (i heard about an "UNIQUE" to add somewhere, tried on UNIQUE VALUES but it didn't work).

- People can still register with spaces only

 

Could you help me, please?

I have the feeling that my code is getting bigger and bigger, is it normal or i'm going the bad way?

Alright, working now! Thanks! :D

I let all those str_replace because trim is working only on the start/end of characters. So if someone put: super            pseudo it will be transformed to: super pseudo

 

Now, the last problem, how can I protect the pseudos/mail. I mean, I don't want people to register with the same pseudo/mail?

I gotta select from the database all the pseudo/mails and compare with the new entries, but I really have no idea how to o.O

 

Tried to add this:

 

$listedespseudos = mysql_query("SELECT pseudo FROM membres");

$pseudos = mysql_fetch_assoc($listedespseudos);

elseif (ereg($_POST['pseudo'],$pseudos['pseudo'])) {

echo "Pseudo déjà utilisé!";

}

 

But not working:

Parse error: syntax error, unexpected T_ELSEIF in C:\Users\Sylvain\Desktop\apprentissagePHP\insertion.php on line 28

 

Is it the way I should go?

You could try:

 

$_POST['pseudo'] = trim($_POST['pseudo']);
if(isset($_POST['pseudo']) && $_POST['pseudo'] != '') {

 

I realize this was back on page 1, but I wanted to comment on it nonetheless to prevent issues for others. The isset() in that code is worthless. The line before that you are explicitly setting a value for $_POST['pseudo'], so it will always be "set" once the if() condition is tested. A better method, in my opinion is as follows:

$pseudo = (isset($_POST['pseudo'])) ? trim($_POST['pseudo']) : false;
if(!empty($pseudo)) {

I let all those str_replace because trim is working only on the start/end of characters. So if someone put: super            pseudo it will be transformed to: super pseudo

 

Instead or running all those str_replace() function. You could do the same thing with one preg_replace() function. For more information on regular expressions and the function, visit http://php.net/manual/en/function.preg-replace.php

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.