•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 375,207 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,300 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1755 | Replies: 4
![]() |
•
•
Join Date: Jan 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
Hi I want to split a variable to check what type of user they are. If they are user type 1 their username will be in the form "LLL" where L is a letter. If they are user type 2 their username will be in the form "LLNNNNN" where L is a letter and N is a number. I thought I could use PHP split and then is_numeric function to check the username. Would this be the best way to do it? If so what is the code I need to use to split the variable?
Many thanks for your help
NH
Many thanks for your help
NH
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,057
Reputation:
Rep Power: 8
Solved Threads: 229
If "LLL" and "LLNNNN" are the 'standard' format, then you can use strlen to check the string length. If string length is 3, then its usertype1. If its 6, the usertype is 2. Well, this is the simplest way. But if you want to thoroughly check, you can explode the variable into an array, (so the array will be $arr[0]="L"; $arr[1]="L"; $arr[2]="L"; and so on ). Then foreach array element, check if its a character or a number. If the character count is 3 and number count is 0, then the usertype is 1. If character count is 2 and number count is 4, then its usertype2.
It really depends on what logic you want to use in your script.
It really depends on what logic you want to use in your script.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: May 2008
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 5
The cleanest way to handle this is:
For lots of regex goodness go here for tutorial. It's for .Nut but regex works the same way for any perl compatible regex and this tutorial is really easy to understand compared to others.
Insight into this particular regex.
each significant part is followed by the explanation in brackets:
/^[from start of string]\w{2}[expect 2 word characters]\d{5}[then 5 numeric digits]$[then end of string]/
This way 1234-ha12345-3242lkj won't match, even though there is a LLNNNNN substring in it.
preg_match returns boolean so you can use it wherever true or false will trip a condition, like in an if, or while, whatever. It is very flexible and powerful but not the most efficient.
$id='ha12345';
switch(true)
{
case preg_match("/^\w{2}\d{5}$/",$id):
//user is LLNNNNN
echo "LLNNNNN";
break;
case preg_match("/^\w{3}$/",$id):
//user is LLL
echo "LLL";
break;
default:
//put login error here. use "username or password" is wrong so they
//can't brute-guess your id format or ids. You also don't want to mention
//"incorrect format".
}For lots of regex goodness go here for tutorial. It's for .Nut but regex works the same way for any perl compatible regex and this tutorial is really easy to understand compared to others.
Insight into this particular regex.
each significant part is followed by the explanation in brackets:
/^[from start of string]\w{2}[expect 2 word characters]\d{5}[then 5 numeric digits]$[then end of string]/
This way 1234-ha12345-3242lkj won't match, even though there is a LLNNNNN substring in it.
preg_match returns boolean so you can use it wherever true or false will trip a condition, like in an if, or while, whatever. It is very flexible and powerful but not the most efficient.
Last edited by rgviza : May 15th, 2008 at 2:52 pm.
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,057
Reputation:
Rep Power: 8
Solved Threads: 229
•
•
Join Date: May 2008
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 5
forgot to mention the security and language compatibility benefit... \w will match non-english characters if locale is set (usually with a language pulldown)
The security benefit is such a login check immunizes you against sql and XSS injection on your login form field by locking in the allowable patterns and size. The approach of the OP was a good one even if he needed a little help with details...
If you use php to hash the user password input instead of mysql's password function, you break sql injection on the password, since you will match passwords on the hashed value, not plain text in the query using mysql's password() function. An injection attempt will just get gobbled. You also avoid sending a plaintext password over your network if the mysqld is on another server.
-Viz
The security benefit is such a login check immunizes you against sql and XSS injection on your login form field by locking in the allowable patterns and size. The approach of the OP was a good one even if he needed a little help with details...
If you use php to hash the user password input instead of mysql's password function, you break sql injection on the password, since you will match passwords on the hashed value, not plain text in the query using mysql's password() function. An injection attempt will just get gobbled. You also avoid sending a plaintext password over your network if the mysqld is on another server.
-Viz
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- how to format the form textarea? (PHP)
- Open In New Window Php (PHP)
- storing multiple email address problem (PHP)
- PHP 'explode()' in C++ ? (C++)
- Equivalent ASP Snippet for PHP Spam Trap (ASP)
- If...else statements (Python)
- php arrays (PHP)
- Help with programming assignment (Python)
- This ought to be simple - extra spaces (PHP)
Other Threads in the PHP Forum
- Previous Thread: elow guyz.......
- Next Thread: How to php export to excel



Linear Mode