I'm having the problem that i want to echo the class $password as often, as the number of $intcount is. Can you give me any idea how I would do that?
f.e. that I echo 4 random words if $intcount is 4, 5 words if its 5 etc etc.
<?php
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
$rand = mt_rand( $min , $max );
$out = $csv[$rand][$rand];
$password = $out;
// var_dump($count);
// var_dump($intcount);
//
// echo $count . "\n <br>";
// echo $password * $intcount;
//
// for ($i=0; $i < $intcount ; $i++) {
// echo $password;
// }
?>
The commented code doesn't need to stay necessarily.
Thanks :)
You need to create the $rand inside the loop like:
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
for ($i=0; $i < $intcount ; $i++)
{
$rand = mt_rand($min, $max);
$password = $csv[$rand][$rand];
echo $password;
}
Related
I build a webpage to crack simple MD5 hash of a four digit pin for fun. The method I used was basically try all combination and check against the MD5 value the user has entered. Below is the PHP code I created to accomplish the goal.
Debug Output:
<?php
$answer = "PIN not found";
if (isset($_GET['md5'])) {
$txt = 'abcdefjhig';
$time_pre = microtime(TRUE);
$value = $_GET['md5'];
$show = 15;
for ($i = 0; $i <= 9; $i++) {
$first = $i;
for ($j = 0; $j <= 9; $j++) {
$second = $j;
for ($k = 0; $k <= 9; $k++) {
$third = $k;
for ($x = 0; $x <= 9; $x++) {
$fourth = $x;
$whole = $first . $second . $third . $fourth;
$check = hash('md5', $whole);
if ($check == $value) {
$answer = $whole;
echo "The pin is $answer";
}
if ($show > 0) {
print"$check $whole \n";
$show = $show - 1;
}
}
}
}
}
echo "\n";
$time_post = microtime(TRUE);
print "Elapsed time:";
print $time_post - $time_pre;
}
?>
Notice that in the middle there are four very similar for loops,I tried to simplify this by using functions, but it just returns one four digit number which is 9999 instead of all of them.
Below is the function I created:
function construct($input){
for($i=0; $i<=9, $i++){
$input=$i;
}
return $input
}
Then I tried to call this function for four times to form all of the four digit numbers but it just gives me 9999. Can anybody help? Thanks a lot.
Try this:
for ($i=0; $i<=9999 ; $i++) {
$whole = sprintf('%04d', $i);
$check=hash('md5', $whole);
if($check==$value){
$answer=$whole;
echo "The pin is $answer";
break; //remove this if you do not want to break the loop here
}
if ($show>0) {
print"$check $whole \n";
$show=$show-1;
}
}
You're using numbers from 0 to 9999, so why just loop through those numbers? You can add zero's by using str_pad() like so:
for ($i = 0; $i <= 9999; $i++) {
$whole = str_pad($i, 4, '0', STR_PAD_LEFT);
}
Also I'd like to mention that you really should think about better formatting of your code, especially indentation
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
here I am trying a random url with the variable $user and $mail with time intervals per 30 minutes.
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
output "site.com/$user" or "site.com/$mail"
from the results of the above output I try to randomly use a loop by calling the variable $y in array $arr = array($y); but the results that come out in the loop "site.com/$user" not "site.com/username"
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
I'm breaking this into multiple steps here. You might be able to condense it:
$x = []
$userElement = 'site.com/'.$user;
$mailElement = 'site.com/'.$mail;
array_push($x, $userElement);
array_push($x, $mailElement);
var_dump($x);
Im am trying to get the min and max temp value from a json src. I'm not getting the desired result that I am looking for. This is what i have so far.
Any help is greatly appreciated
<?php
$url = 'https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22';
$data = file_get_contents($url);
$forecasts = json_decode($data);
$length = count($forecasts->list);
for ($i = 0; $i < $length; $i++) {
$val = round($forecasts->list[$i]->main->temp, 0);
}
$min = min($val);
$max = max($val);
echo "max: ". $max ." - min: ". $max;
?>
You are overwriting the value of $val in each pass through your for loop. What you actually want to do is push each value into an array that you can then take the min and max of:
$val = array();
for ($i = 0; $i < $length; $i++) {
$val[] = round($forecasts->list[$i]->main->temp, 0);
}
$min = min($val);
$max = max($val);
Ok so I'm assuming that "temp" is the attribute where you want to get min/max:
<?php
function max($list){
$highest = $list[0]->main->temp;
foreach($list as $l){
$highest = $l->main->temp > $highest ? $l->main->temp : $highest;
}
return $highest;
}
function min($list){
$lowest = $list[0]->main->temp;
foreach($list as $l){
$lowest = $l->main->temp < $lowest ? $l->main->temp : $highest;
}
return $lowest;
}
$url = 'https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22';
$data = file_get_contents($url);
$forecasts = json_decode($data);
$length = count($forecasts->list);
$min = min($forecasts->list);
$max = max($forecasts->list);
echo "max: ". $max ." - min: ". $max;
?>
should be copy-pastable for your case...
How do I make sure that the password contains at least one character from each array? I could use do..while, right? What I don't quite understand is what I will need to put in the if statement inside do...while.
Is do...while the only way to ensure at least one character from each array is in the final password or are there other ways?
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$resultArr[] = $a1[array_rand($a1)];
$resultArr[] = $a2[array_rand($a2)];
$resultArr[] = $a3[array_rand($a3)];
for($i = 0; $i < $length-3; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$resultArr[] = $values[$chosen][array_rand($values[$chosen])];
}
shuffle($resultArr);
$result = implode($resultArr); //final random password
echo $result;
You can surround the for wit ha do..while and in the condition check with preg_match() and a regex pattern that checks if at least one character is in the result:
<?php
$length = 6;
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$result = ""; //final random password
do {
for($i = 0; $i < $length; $i++){
$values = [$a1,$a2,$a3];
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
} while(!(preg_match('/^(?=.*?[0-9])(?=.*?[a-z])(?=.*?[%*+?!]).{1,}/', $result)));
echo $result;
If the there's no coincidences with the pattern using preg_match, then will return false, in the do..while condition we check if one preg_match is false, if it is, do it again.
I made my answer general for any number of arrays.
I would use preg_match() for this, but for the question you asked, yes, you need to check each array separately:
$user_inputted_password = $_POST['user_inputted_password'];
$length = strlen($user_inputted_password); // user input password - you are capturing this somewhere, right
$a1 = str_split('0123456789');
$a2 = str_split('%^*+~?!');
$a3 = str_split('abcdefghigklmnopqrstuvwxyz');
$validate = array();
$result = ""; //final random password
function populate_validation_array($match_num, &$validate) {
if (!in_array($match_num, $validate)) {
$validate[] = $match_num;
}
}
$numeric_indexes = array(1, 2, 3);
$index_count = count($numeric_indexes);
for($i = 0; $i < $length; $i++){
for($j = 0; $j < $index_count; $j++) {
$numeric_index = $numeric_indexes[$j];
if (in_array($user_inputted_password[$i], ${"a" . $numeric_index})) {
populate_validation_array($numeric_index, $validate);
}
}
}
if (count($validate) === $index_count) {
$values = array();
for($i = 0; $i < $index_count; $i++) {
$values[] = ${"a" . ($i + 1)};
}
$chosen = array_rand($values);
$result .= $values[$chosen][array_rand($values[$chosen])];
}
echo $result;
I have a 6x6 bingo card which is generated with random numbers between 10 and 70. The 7th row and 7th column are used to count the drawn numbers that are on the card. When a row or column reaches 6, there is bingo.
The eventual result I get is right, but in the proces of getting there a few things are going wrong.
In my function generateCard I create the rows and columns of numbers for the card. I think the problem lies in this function.
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
I've put in a few printCard functions to test the outcome.
The first test above the creation of the 7th column gives me a ton of undefined offset 7 notices. I figured this happens because the column does not exist, but when I try to create this earlier, I still get the notices.
The second test above the 7th row shows that the 7th row (which should be 0) gets filled with numbers. This gets overwritten after with 0 values. I figured I could fix this by putting $row < 7 and $kolom < 7, but when I do this the card won't get printed right at all.
I'm wondering why i'm getting all these undefined offset notices about the last column (even when I create it earlier) and why I can't use $row < 7 and $column < 7 in both the for loops of generate card to avoid filling up the last row and column (7x7) with values. These should be 0 before the bingo game starts.
Do you have any suggestions?
I think i'm overlooking a few things here..
I will put the complete code here:
mt_srand((double)microtime()*1000000);
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php for($rij = 1; $rij < 8; $rij++) { ?>
<tr>
<?php for ($kolom = 1; $kolom < 8; $kolom++) { ?>
<td<?php if (($card[$rij][7] == 6) || ($card[7][$kolom] == 6)) { echo ' style="background-color:green"'; } ?>><?php echo $card[$rij][$kolom]; ?></td>
<?php }
} ?>
</tr>
</table>
<?php }
$card = generateCard();
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
for ($row = 1; $row < 7; $row++) {
for ($kolom = 1; $kolom < 7; $kolom++) {
if ($card[$row][$kolom] == $number) {
$card[$row][7] += 1;
$card[7][$kolom] += 1;
if(($card[$row][7] == 6) || ($card[7][$kolom] == 6)){
$bingo = true;
}
break;
}
}
}
}
}
echo '<h2>Bingokaart waarop BINGO is gevallen</h2>';
printCard($card);
echo '<p><strong>Getrokken getallen:</strong><br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
echo '<p><strong>Aantal getallen dat is getrokken:</strong> ';
echo count($getrokkenGetallen);
echo '</p>';
Example of the output:
Thank you in advance for any help or suggestions.
I'm not exactly sure where your error comes from, and I'm not a regular PHP user, but if i change the generateCard function to this:
function generateCard(){
$card = array();
for ($row = 1; $row < 8; ++$row){
$card[$row] = array();
$deck = range($row*10, $row*10+9);
shuffle($deck);
for($col = 1; $col < 8; ++$col){
if($row == 7 OR $col == 7){
$card[$row][$col] = 0;
}else{
$card[$row][$col] = $deck[$col];
}
}
}
//Test
printCard($card);
return $card;
}
It all works as expected. It's a little simpler than the route you were going i think.