Link to home
Start Free TrialLog in
Avatar of phaygarth
phaygarth

asked on

Display random text file to visitor, but same file if they refresh the page.

I've been trying to find a script that can do the following;

I'd like to display the contents of a random text file in a box on a webpage.

There are 80 different text files in total, that I will store in a folder.

Each visitor should see a random file when they first visit, but if they then refresh the page, I want the same file to be displayed to them. (For a max period of 24 hours say.)

It should also display the name of the text file displayed.

Any help you can give is greatly appreciated.

Thanks
Avatar of mankowitz
mankowitz
Flag of United States of America image

you want to save the text file in the session and then re use it as needed. For example

session_start();
if (!isset($_SESSION['filename'])) {
   // pick a random file
   $_SESSION['filename'] = rand().....;
}

   // show the file that you want
   // for example
   echo file_get_contents($_SESSION['filename']);
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can change session lifetime if you need to:

ini_set('session.gc_maxlifetime', 84600);
session_set_cookie_params(84600);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ray, of course, the user could edit the cookie to see a different file, which is harder than faking a sessionID
@mankowitz: agree, and there are ways that we can reduce the risk of cookie tampering.  Most frameworks already implement something like this.  With broad questions like this one there are always many layers to the solutions!

<?php // cookie_security.php

/**
 * Demonstrate how to encode information in a cookie
 * to reduce the risk of cookie tampering
 *
 * A salted message digest is included with the cookie
 *
 * If the message digest does not match the value of the cookie
 * we can assume that the cookie has been damaged and we can
 * discard it
 */
error_reporting(E_ALL);

// A DATA DELIMITER
$dlm = '|';

// YOUR OWN SECRET CODE - THE 'SALT' STRING
$secret_code = 'MY SECRET';

// A DATA STRING THAT WE WANT TO STORE (MIGHT BE A DATABASE KEY OR SIMILAR)
$cookie_value = 'MARY HAD A LITTLE LAMB';

// ENCODE THE DATA STRING TOGETHER WITH OUR SECRET
$cookie_code = md5($cookie_value . $secret_code);

// CONSTRUCT THE COOKIE STRING WITH THE CLEAR TEXT AND THE CODED STRING
$safe_cookie_value = $cookie_value . $dlm . $cookie_code;

// SET THE COOKIE LIKE "MARY HAD A LITTLE LAMB|cf783c37f18d007d23483b11759ec181"
setcookie('safe_cookie', $safe_cookie_value);



/**
 * WHEN STORED, THE COOKIE WILL BE URL-ENCODED SO IT WILL LOOK SOMETHING LIKE THIS ON THE BROWSER
 * MARY+HAD+A+LITTLE+LAMB%7Ccf783c37f18d007d23483b11759ec181
 * IT WILL BE URL-DECODED BEFORE IT IS PRESENTED TO PHP
 */

// HOW TO TEST THE COOKIE
if (isset($_COOKIE["safe_cookie"]))
{
    // BREAK THE COOKIE VALUE APART AT THE DELIMITER
    $array = explode($dlm, $_COOKIE["safe_cookie"]);

    // ENCODE THE DATA STRING TOGETHER WITH YOUR SECRET
    $cookie_test = md5($array[0] . $secret_code);

    // IF THE MD5 CODES DO NOT MATCH, THE COOKIE IS NO LONGER INTACT
    if ($cookie_test == $array[1])
    {
        echo "<br/>THE COOKIE {$_COOKIE["safe_cookie"]} IS INTACT";
    }
    else
    {
        // WHEN THE COOKIE HAS BEEN DAMAGED, DISCARD IT
        echo "<br/>THE COOKIE {$_COOKIE["safe_cookie"]} HAS BEEN CORRUPTED AND CANNOT BE USED";
        $_COOKIE['safe_cookie'] = NULL;
		setcookie('safe_cookie', NULL, time()-86400);
    }
}
else
{
    die('COOKIE IS SET - REFRESH THE BROWSER WINDOW NOW');
}




// MUNG THE COOKIE TO DEMONSTRATE WHAT HAPPENS WITH A CORRUPT COOKIE
$_COOKIE["safe_cookie"] = str_replace('MARY', 'FRED', $_COOKIE["safe_cookie"]);

// HOW TO TEST THE COOKIE
if (isset($_COOKIE["safe_cookie"]))
{
    // BREAK THE COOKIE VALUE APART AT THE DELIMITER
    $array = explode($dlm, $_COOKIE["safe_cookie"]);

    // ENCODE THE DATA STRING TOGETHER WITH OUT SECRET
    $cookie_test = md5($array[0] . $secret_code);

    // IF THE MD5 CODES DO NOT MATCH, THE COOKIE IS NO LONGER INTACT
    if ($cookie_test == $array[1])
    {
        echo "<br/>THE COOKIE {$_COOKIE["safe_cookie"]} IS INTACT";
    }
    else
    {
        echo "<br/>THE COOKIE {$_COOKIE["safe_cookie"]} HAS BEEN CORRUPTED AND CANNOT BE USED";
    }
}

Open in new window