Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,720 members, 7,813,372 topics. Date: Tuesday, 30 April 2024 at 11:26 AM

Php Random Script To Generate Pin No. - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Php Random Script To Generate Pin No. (8391 Views)

Free Script To Check Domain Availability / How do i add A Free Sms Script To My Site So : / [javascript ]- A Script To Generate Random Colors (2) (3) (4)

(1) (Reply) (Go Down)

Php Random Script To Generate Pin No. by ajaxphp7: 11:22am On Jun 25, 2010
Guys in the house please can anyone help with a php script that can generate Pin code at random without repeatation.
Re: Php Random Script To Generate Pin No. by DualCore1: 1:15pm On Jun 25, 2010
Use the php rand() function to generate random numbers.
Check the database (you have created for storing numbers) for an occurrence of the number generated.
If it exist in the database, generate another
Run through that cycle.
When it doesn't exist in the database
Store it in the database and use for whatever you want it for.


On a site I am developing, users are supposed to register and have a unique PIN sent to their email.
I store the pin in a database but I don't counter check it during generation to know if it exists.
What I do is take the first 2 letters of the user's first name and add it to a randomly generated number.
I don't see how there will ever be duplicates of that.

Here's what my code looks like

$initials = substr($firstname, 0, 2);
$id = $initials.rand();

Line 1 takes the first the characters in the user's first name which i chose to call the initials
Line 2 takes the initials and concatenates it with a randomly generated set of numbers and I choose to store it as a variable called "id"

Using "ken" as my first name, if you echo $id, the output will be something like "ke85793";

1 Like 1 Share

Re: Php Random Script To Generate Pin No. by onyengbu1(m): 3:25pm On Jun 25, 2010
Dual Core:


What I do is take the first 2 letters of the user's first name and add it to a randomly generated number.
I don't see how there will ever be duplicates of that.

Never say never grin

You gave the guy a great advice which u are obviously too tired to follow yourself.
Re: Php Random Script To Generate Pin No. by DualCore1: 3:48pm On Jun 25, 2010
My brother, how we go do. Ok, I am promising to do it the right way the next time I ever have to do it on any project. I am already concluding this already smiley
Re: Php Random Script To Generate Pin No. by Nobody: 4:21pm On Jun 25, 2010
@Dual. Bros, I may need help with that too, I have never explored such, and will be developing a student portal that will need random ID generation, Shey u go assist with codes?
Re: Php Random Script To Generate Pin No. by ajaxphp7: 4:33pm On Jun 25, 2010
Thanks very much for the contribution. I am really grateful but this is a school portal for a commercial secondary school. The answer to to the question doesn't really gives me what i need. I will need to generate pin no at random so that at the purchase of the cards users will be able to register with it. A unique pin No which can not be used by another person.
thanks I really appreciate
Re: Php Random Script To Generate Pin No. by ajaxphp7: 4:35pm On Jun 25, 2010
Any contribution will be appreciated as I am stock with the coding now.
Re: Php Random Script To Generate Pin No. by DualCore1: 7:08pm On Jun 25, 2010
ajaxphp7:

Thanks very much for the contribution. I am really grateful but this is a school portal for a commercial secondary school. The answer to to the question doesn't really gives me what i need. I will need to generate pin no at random so that at the purchase of the cards users will be able to register with it. A unique pin No which can not be used by another person.
thanks I really appreciate
Brother you are a developer, think wild and make things happen for you!

This is a continuation of my post.

In the database where you will store the pins generated, add a field called "status" and a primary.

store the student's unique ID in a variable
store pin in a variable

if(user enter pin){
    select status from pins where pin = pin
        if(status = "used" or pin is tied to a different ID other that the student ID entered){
                echo "u no well o, c'mon go and buy new scratch card  angry "
                 header('Location: anywhere.php');
           }
       else{
                  header('Location: rightplace.php');   
                  set the status of this pin to "used" and tie that very pin to the students ID
              }

}

In the pin validation part of your code.

when user enters pin, check the status of the pin in the DB, if the pin's status is used, throw any kinda error u want at the user.
if its not used, voila!

during pin generation set all pin status to "not used".

My explanations are very rough but its for you to sit down with it and try to develop a solution out of it.

Donpuzo:

@Dual. Bros, I may need help with that too, I have never explored such, and will be developing a student portal that will need random ID generation, Shey u go assist with codes?
LoL why are we all about to develop the same thing. I will be developing a school portal after I am done with my next job. I dunno details yet, its actually a developer who took up the job and has dived into bricks so I have to dev it for him.
Re: Php Random Script To Generate Pin No. by Nobody: 8:57pm On Jun 25, 2010
my contribution is that. for the table that i want to create the random number from, i will have an autoincrement field such as

pid
Now when i am going to generate the next pin, i will first get the last generated autoincrement value:
$res=mysql_query(select max(id) from pintable); . . . yada. . .yada. . .yada
Now i will use something like mt_rand and then add the [max(id)+1] returned - into any position -
$maxid.mt_rand().mt_rand() . . . or any particular pattern,
so that if all else are the same, letz say the mt_rands return the same thing (impossible), the final pin value will still be the same

well, i hope i have said something useful. And of course, remember that the value of an autoincrement id is always unique.

Finally, the field you wish to store the pin must have the unique attribute so that, if all else fails, no pin code will be repeated - the dbase engine will
make sure of that.

Please not that, i am speaking in general. i assume your project is php/mysql = if it is php/oracle or any other combination, then the information changes slightly
Re: Php Random Script To Generate Pin No. by webaplanet(m): 10:24am On Jun 26, 2010
i have a complete script that generates random numbers-it was wriiten for that pupose--to generate pinnumbers for a school portal, the pin is written into a table in mysql we then transfer the pin to access database from there we print to paper,cut the paper into recharge card size and use foil to cover the pin area, if you are interested send sms to 0708 723 3235
Re: Php Random Script To Generate Pin No. by webaplanet(m): 10:25am On Jun 26, 2010
sms 0708 723 3235
Re: Php Random Script To Generate Pin No. by Nobody: 10:26am On Jun 26, 2010
lol
Re: Php Random Script To Generate Pin No. by Nobody: 2:00pm On Jun 26, 2010
@webaplanet, Bros shey na recharge card sales the guy won start?
Re: Php Random Script To Generate Pin No. by DualCore1: 2:27pm On Jun 26, 2010
Donpuzo:

@webaplanet, Bros shey na recharge card sales the guy won start?



webaplanet:

cut the paper into recharge card size and use foil to cover the pin area

Take note of what I have put in bold.
Re: Php Random Script To Generate Pin No. by Morrelinko(m): 11:46pm On Jun 10, 2012
hey, i was doing some work when i saw this post and decided to write something for you...

[php]

/*

the first argument is the number of pair groups
the 2nd argument is the number of letters to pair
each group is separated with a hypen (-).
Hope this helps you in your project.. Good night

*/

function generate_key($group_num = 4, $pair_num = 2)
{
$letters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$key = '';

for($i = 1; $i <= $group_num; $i++)
{
$key .= substr($letters, rand(0, (strlen($letters) - $pair_num)), $pair_num) . '-';
}

$key[strlen($key)-1] = ' ';

return trim($key);
}

# Example usage;

echo generate_key(4, 4);

[/php]

1 Like

Re: Php Random Script To Generate Pin No. by Morrelinko(m): 11:49pm On Jun 10, 2012
use the code to generate the pin then use the tip from dual core to create your pin system.
Re: Php Random Script To Generate Pin No. by talk2me006(m): 7:52am On Jun 11, 2012
webaplanet: i have a complete script that generates random numbers-it was wriiten for that pupose--to generate pinnumbers for a school portal, the pin is written into a table in mysql we then transfer the pin to access database from there we print to paper,cut the paper into recharge card size and use foil to cover the pin area, if you are interested send sms to 0708 723 3235
Let us meet at 2go.we need to talk.08039685053
Re: Php Random Script To Generate Pin No. by talk2me006(m): 8:17am On Jun 11, 2012
ajaxphp7: Any contribution will be appreciated as I am stock with the coding now.
hey! i know hw u feel. i can help u wt a software dat wil generate unique scratch card pin and serial no for u within 30secs without coding.
u wil just export dem to ur database and use dreamweaver to programme it to be valid for respective students user.
i used d technique above to design a website for one department in my university. u can sell each card at d rate of 500 naira and they wil realy apreciate it becos every students hav to enter to his/her portal b4 he/she can gain acces to check his/her results.
nw i hav many departments on my neck to build for dem.
contact me for more help- 08039685053
Re: Php Random Script To Generate Pin No. by Nobody: 9:55am On Jun 11, 2012
please consider reading up on pseudo random versus Real Random

the best practice is appending with a substring from the primary key, and doing a check before inserting like someone suggested.

1 Like

Re: Php Random Script To Generate Pin No. by talk2me006(m): 12:23pm On Jun 30, 2012
webaplanet: i have a complete script that generates random numbers-it was wriiten for that pupose--to generate pinnumbers for a school portal, the pin is written into a table in mysql we then transfer the pin to access database from there we print to paper,cut the paper into recharge card size and use foil to cover the pin area, if you are interested send sms to 0708 723 3235

pls bros, i only want to know how to get the foil to cover my scratch card .
i mean, i have already print out my card but only need to cover d pin and serial no with d foil.
pls can u help me out.
this is my phone n0 08039685053
Re: Php Random Script To Generate Pin No. by matthew80(m): 1:54pm On Mar 08, 2015
Morrelinko:
hey, i was doing some work when i saw this post and decided to write something for you...
[php]
/*
the first argument is the number of pair groups
the 2nd argument is the number of letters to pair
each group is separated with a hypen (-).
Hope this helps you in your project.. Good night
*/
function generate_key($group_num = 4, $pair_num = 2)
{
$letters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$key = '';
for($i = 1; $i <= $group_num; $i++)
{
$key .= substr($letters, rand(0, (strlen($letters) - $pair_num)), $pair_num) . '-';
}

$key[strlen($key)-1] = ' ';

return trim($key);
}

# Example usage;

echo generate_key(4, 4);

[/php]

Great job though. But, do you know that the numbers generated cannot be inserted into the database as a string i.e. a single value, but instead, the system reads it as four different values (e.g. from your #Example usagewink, and would be inserted into four different columns...if at all. In order to generate the number as a string, i.e. real pin, think about trimming off the special characters and using the explode() function to convert the array to string before proceeding to the db insert section. Hope it helps.

(1) (Reply)

How Often Do You Google Yourself? / Post All Your Web Development Issues Here (php, Sql, Html, Javascript, Css) / How To Setup A Custom Domain Name For Your Blogger Blog

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.