0

I searched all over, but have not seen anything related to this question. I have a curl script that obtains a random generated string of 10 characters from a website. I have received permission from the website owner to fetch this data and display it on my blog. My question is this when a user comes to my website it displays the string and if they refresh the page it will display a new string. How can I limit one string per user so that they cannot refresh over and over to obtain multiple strings?

Here is an example curl upon which my code is based:

$url = "http://m.www.yahoo.com/";  
$ch = curl_init();  

curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  

$output = curl_exec($ch);  

preg_match('/<dl class="markets clearfix strong small">(.*)<\/dl>/is', $output, $matches);  
echo $matches[0];  

curl_close($ch);  
sarsar
  • 1,431
  • 4
  • 14
  • 17
  • 2
    Have you tried session or cookies? Or if you want more advanced solution, just require from user to log in and set generated string to some of user's field. – Robik Jul 12 '11 at 15:59
  • You can go a step farther and use the database, memcache, or a file as a counter per ip/user agent combo so the user cant just clear the cookie to get a new set of curl requests. – Geoffrey Wagner Jul 12 '11 at 16:04
  • @Robik Would I place `$matches[0];` in a sessions? – sarsar Jul 12 '11 at 16:05

1 Answers1

0

maybe you can use session variable to check if the user has already got a string.

function startSession(){
    $id = session_id();
    if ($id != '')
        session_write_close();

    if (isset($_COOKIE['PHPSESSID']) && $id != $_COOKIE['PHPSESSID']) {
        session_write_close();
        session_id($_COOKIE['PHPSESSID']);
    }

     session_start();
}

//Start your sessions

startSession();
if (!$_SESSION['UserGotAString']) {
    echo "Some Error msg"
    return;
}
session_write_close();

//Request to Yahoo and regex match comes here..

// Start a session
startSession();
$_SESSION['UserGotAString'] = true
echo $matches[0];
session_write_close();

Im not sure if this a good method. Also if you want to reset the session variable base on timer check this link Set timeout in php

Community
  • 1
  • 1
pravin
  • 1,106
  • 1
  • 18
  • 27