How to do alphanumeric password

I am trying to do an alphanumeric password for my signup page and tried to insert this code:

if (Strlen($password) < 5 OR !preg_match("/^[a-zA-Z0-10]*$/", $password)) {
                   header("Location: ../signup.php?signup=invvalidlength");
                   exit();

But it will still allow me to register

If you want to ensure a password contains at least one non-alphabetic non-numeric character I think ctype_alnum could work until you understand regex better.

http://php.net/manual/en/function.ctype-alnum.php

(no offense intended, but this is as a learning excercise and not a live site, correct?)

Hello!

I tried to put that code into my site but it doesn’t work…

if (!ctype_alnum($password)){
                   	   header("Location: ../signup.php?signup=invvalidalphanumeric");
                   	   exit();

What does that mean? What does it do that you don’t want, or what does it not do that you do want?

2 Likes

I am trying to direct the user to an error page if it is not alphanumeric… the variable $password is the password that the user input in the form

So are you saying that it doesn’t redirect to the error page if the password contains punctuation marks, spaces and other unwanted characters?

Rather than trying the redirect, just echo the value of ctype_alnum($password) for different passwords, to see what values the function is returning.

… or have you already sent something to the browser before you get to the header() and it’s failing because headers have already been sent?

1 Like

Ok… i just tested out the code and typed

<? php

$password = 'password123';
echo ctype_alnum ($password );

Came back displaying 1, which is true… so I ran the following code

If (!ctype_alnum ($password)  == 1) {
  header ("Location: ../signup.php?signup=notalphanumeric
} else {

//Proceed checking

and what happened?

I think maybe i should try…

if (ctype_alnum ($password) != 1) {
// redirect user to error

}

Try this

if (strlen($password) < 5 || strlen($password) > 20) {

    // password must be between 5 and 20 characters long
    header("Location: ../signup.php?signup=invvalidlength");
    exit();

} else if (!ctype_alnum ($password)) {

    // password does not consist of all letters or digits
    header ("Location: ../signup.php?signup=notalphanumeric");
    exit();

}

Not seeing your exit statement after the header call?

i have tried this and it still doesn’t work which is weird be

if (Strlen($password) < 5) {
                   header("Location: ../signup.php?signup=invvalidlength");
                   exit();
                   } else {
                   if (!ctype_alnum($password)){
                   	   header("Location: ../signup.php?signup=invvalidalphanumeric");
                   	   exit();
                 	 } else {

I have also tried it with this function

if (!preg_match("/^[a-zA-Z0-9]*$/", $password)){
                   	   header("Location: ../signup.php?signup=invvalidalphanumeric");
                   	   exit();

but it still doesn’t work…

<off-topic>
@piano0011, when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.
</off-topic>

1 Like

But in what way? Does it redirect when it should not, or not redirect when it should, or give the wrong error message. Tell us how it doesn’t work. And/or show the rest of the code so we can see if something prior to this is stopping it from doing what you expect it to.

1 Like

It must work, add just after

<?php 

error_reporting(E_ALL);

// other code 

and tell us error or post whole script it will be easier.

Or you can user preg_match if you are better with that

<?php

error_reporting(E_ALL);

if (strlen($password) < 5 || strlen($password) > 20) {

    // password must be between 5 and 20 characters long
    header("Location: ../signup.php?signup=invvalidlength");
    exit();

} else if (!preg_match('/^[a-zA-Z0-9]+$/', $password)) {

    // password does not consist of all letters or digits
    header ("Location: ../signup.php?signup=notalphanumeric");
    exit();

}

Strictly speaking, the urls for redirects are supposed to be absolute. Something like

header ("Location: https://example.com/project/signup.php?signup=notalphanumeric");

Though I think most browsers can handle relative urls. But as quite a few people have already mentioned, “does not work” really “does not help”.

5 Likes

I will try it again later but should this code work?

if (ctype_alnum ($password) != 1){
   echo 'sorry but it has to be alphanumeric';
} else {
   echo 'ok';
}

In isolation there doesn’t seem to be anything wrong with it.

No no a thousand times no.

Look at the docs: http://php.net/manual/en/function.ctype-alnum.php

ctype_slum returns a boolean, not a number. Yes php supports mixing types. Does that mean you should do so? A most emphatic no. If a function returns a boolean then check for true or false. It will save you a huge amount of trouble. Especially before you understand how to organize your code.

if (ctype_alnum ($password) === false) {