samedi 9 mai 2020

PHP Check if a string is not contained in a document

I want to check if the 'name' is already written in a HTML document. If it is not already there a random color should be generated. Is the code correct? Because when I reload the page it will generate a new random color even though the name is in the HTML document.

function random_color_part() {
    return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
    return random_color_part() . random_color_part() . random_color_part();
}

$color = "";
$filename = 'log.html';
$searchfor = $_POST ['name'];
$fh = fopen($filename, 'r');
$olddata = fread($fh, filesize($filename));
if (!strpos($olddata, $searchfor) !== false) {
    $color = random_color();
}

Thanks for the help.

Aucun commentaire:

Enregistrer un commentaire