Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2008, 11:38 PM   PM User | #1
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
Simple password generator

Demo: http://p4plus2.co.cc/pass/password.php

source:
PHP Code:
<html>
<head>
<title></title>
</head>
<body>
<?php
$passwordgenerate 
= array(
=> "`",
=> "~",
=> "!",
=> "@",
=> "#",
=> "$",
=> "%",
=> "^",
=> "&",
10 => "*",
11 => "(",
12 => ")",
13 => "_",
14 => "-",
15 => "=",
16 => "+",
17 => "{",
18 => "}",
19 => "[",
20 => "]",
21 => "|",
22 => "\\",
23 => ":",
24 => ";",
25 => "\"",
26 => "'",
27 => "/",
28 => "?",
29 => ".",
30 => ">",
31 => "<",
32 => ",",
33 => "0",
34 => "1",
35 => "2",
36 => "3",
37 => "4",
38 => "5",
39 => "6",
40 => "7",
41 => "8",
42 => "9",
43 => "q",
44 => "w",
45 => "e",
46 => "r",
47 => "t",
48 => "y",
49 => "u",
50 => "i",
51 => "o",
52 => "p",
53 => "a",
54 => "s",
55 => "d",
56 => "f",
57 => "g",
58 => "h",
59 => "j",
60 => "k",
61 => "l",
62 => "z",
63 => "x",
64 => "c",
65 => "v",
66 => "b",
67 => "n",
68 => "m",
69 => "Q",
70 => "W",
71 => "E",
72 => "R",
73 => "T",
74 => "Y",
75 => "U",
76 => "I",
77 => "O",
78 => "P",
79 => "A",
80 => "S",
81 => "D",
82 => "F",
83 => "G",
84 => "H",
85 => "J",
86 => "K",
87 => "L",
88 => "Z",
89 => "X",
90 => "C",
91 => "V",
92 => "B",
93 => "N",
94 => "M"
);
$passwordlength $_POST['length'];
$type $_POST['type'];
    function 
type(){
        global 
$type$passwordgenerate$password$passwordlength;
        
$i 0;
        if(
$type == all){
            while(
$i $passwordlength){
                
$number rand(1,94);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == letters){
            while(
$i $passwordlength){
                
$number rand(43,94);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == caps){
            while(
$i $passwordlength){
                
$number rand(69,94);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == low){
            while(
$i $passwordlength){
                
$number rand(43,68);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == numbers){
            while(
$i $passwordlength){
                
$number rand(33,42);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == symbols){
            while(
$i $passwordlength){
                
$number rand(1,32);
                
$password .= $passwordgenerate["$number"];
                
$i++;
            }
        return 
$password;
        }
        elseif(
$type == extreme){
            while(
$i $passwordlength){
                
$number rand(1,94);
                
$password1 md5(str_rot13(md5(sha1($passwordgenerate["$number"]))));
                
$password .= $password1;
                
$i++;
            }
        return 
$password;
        }
        else{
            echo 
"hacking attempt *or a internal error in which case refresh the page*";
            die;
            
        }
    }
    if(
$passwordlength != ''){
        
type();
    }
echo(
"<form action=\"password.php\" method=\"post\">pass length: <input name=\"length\" maxlength=\"4\" value=\""."$passwordlength"."\" /><br />pass type: <select name=\"type\"><option value=\"all\">all</option><option value=\"letters\">a-z(caps and lower)</option><option value=\"caps\">Letters a-z (caps)</option><option value=\"low\">letters a-z(lower)</option><option value=\"numbers\">numbers 0-9</option><option value=\"symbols\">All symbols on keyboard</option><option value=\"extreme\">extreme*</option></select><br /><input type=\"submit\" /></form>");
echo(
"$password");
?>
<p>
<br /><br />
* extreme is longer in length than what you input and is heavily encrypted letters and numbers.!
</p>
</body>
</html>

I know I could have used a for each statement and all those goodies but I did it the long way since it is easier to update in the future. post feedback!

Thanks,
~p4plus2~

Last edited by Inigoesdr; 03-04-2008 at 12:57 AM..
p4plus2 is offline   Reply With Quote
Old 03-04-2008, 01:09 AM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by p4plus2 View Post
I know I could have used a for each statement and all those goodies but I did it the long way since it is easier to update in the future. post feedback!
You could have used a for() loop for the password generation instead of a while(), too. What is going to be easier to update later using this method? Also the global keyword isn't needed since you can pass the values as arguments to the function, or pass them by reference if you intend to modify them.
Inigoesdr is offline   Reply With Quote
Old 03-04-2008, 01:44 AM   PM User | #3
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
I use global since when I deal with phpbb(2.0.*) a lot. we are supposed to use global in our functions for variables. old habits. for and while loops do just about the same thing only real difference is syntax I didn't see any real benefit for a for statement here.

For updating I plan on adding special characters to the array and if I used for each it just becomes a mess since I'll need to redo my loop. Call me crazy as well but I tend to like doing things the hard way gives more satisfaction than a short cut.
p4plus2 is offline   Reply With Quote
Old 03-04-2008, 03:51 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by p4plus2 View Post
for and while loops do just about the same thing only real difference is syntax I didn't see any real benefit for a for statement here.
Using a while() loop like you do here is similar to using a for() loop, however, you can do the same thing this code does:
PHP Code:
$i 0;
while(
$i $passwordlength)
{
     
$number rand(1,94);
     
$password .= $passwordgenerate["$number"];
     
$i++;

Like this:
PHP Code:
for($i 0$i $passwordlength$i++)
     
$password .= $passwordgenerate[rand(194)]; 
This situation is exactly what a for() loop is for.
Inigoesdr is offline   Reply With Quote
Old 03-04-2008, 05:18 AM   PM User | #5
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
Back to my point though what are the real benefits thats what I questioned since they still do the same thing. yah they are small, but do they effect anything, since I never noticed a diff when I used em....and shouldn't the code be:

PHP Code:
for($i 0$i $passwordlength$i++){
     
$password .= $passwordgenerate[rand(194)];

p4plus2 is offline   Reply With Quote
Old 03-04-2008, 05:36 AM   PM User | #6
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
No need for the brackets if the rest of the loop is just one line. The loop will read only the first line if no brackets are in place.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-04-2008, 06:00 AM   PM User | #7
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by p4plus2 View Post
Back to my point though what are the real benefits thats what I questioned since they still do the same thing. yah they are small, but do they effect anything, since I never noticed a diff when I used em
Well, the for() loop is slightly faster(~0.006 secs), and the syntax is better suited for this type of loop, but other than that they're functionally equivalent in this case.
Inigoesdr is offline   Reply With Quote
Old 03-04-2008, 03:13 PM   PM User | #8
p4plus2
Regular Coder

 
Join Date: Mar 2008
Posts: 103
Thanks: 1
Thanked 8 Times in 8 Posts
p4plus2 is an unknown quantity at this point
ah....well I may convert it later when I'm not so busy then but it is lower on my to-do list since it is already working. I have a lot of things I am working on atm so I'd like to get em working first.
p4plus2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:03 AM.


Advertisement
Log in to turn off these ads.