Jump to content

Random username and password from CSV file


young_coder

Recommended Posts

Dear all,

Can somebody help me please?

Instead of ‘username’ and 'password', I need random values from CSV file.

Can somebody show me how can I do this?

 

My CSV file looks like this:

 

user001,userpass001

user002,userpass002

user003,userpass003

 

My script looks like this:

 

<?php

$t= new post();

$t->username='username';

$t->password='password';

$res = $t->update('This is some text.');

?>

 

Thank you very much advance.

 

Hello young_coder,

 

You can use the file() function to read the csv file into an array. You have line breaks in your csv file, and each line will be an element in the array. Then you can use shuffle() to randomize the order of the array, and reset() to pluck off the first element of that array. Lastly, you would call explode() to split the username and password.

 

Cheers

Then you can use shuffle() to randomize the order of the array, and reset() to pluck off the first element of that array.

 

Using shufle() is unnecessary and reset() won't do anything productive since the array is already set to the the first element. Once you have the array of values using file(), just use array_rand() to get a random record from the array

$randomRecord = $array[array_rand($array)];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.