问题描述:

Each time user enter's website get random information/text from mysql.

Question: How to make sure every time user refresh page she/he get difirrent information no the same viewed before?

First idea camed to my mind:

  1. User enter's website, he/she get information which has $id= '123';
  2. That id we store in session
  3. User again refresh page and new if statment executed:

if ($id == $random_id_from_mysql) {

//select new id from mysql, because it's the same, viewed before

} else {

//show new information and store $id in session for feature usage

}

Any new method available for this ?

网友答案:
session_start();

// If a value already exists in $_SESSION, it will be used.
// Otherwise, NULL
$prev_rand = isset($_SESSION['prev_rand']) ? $_SESSION['prev_rand'] : NULL;

// Query for a random record that isn't equal to the previous one 
// (or isn't NULL if no previous one exists)
// using RAND() and LIMIT 1
$result = mysql_query("SELECT id, col1 FROM tbl WHERE id <> '$prev_rand' ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_array($result);

// Store the new rand value into session
$_SESSION['prev_rand'] = $row['id'];
相关阅读:
Top