Iulius
Iulius

Reputation: 314

How can I make a page expire after user visit it?

I have a page http://www.mysite.com/newsletter.php

Now I want to put a variable there when I send you an email and you visit this page to see you email/username there.

So I email you this link http://www.mysite.com/[email protected]

Now when you will access this link you will see you email/username on that page as "Hello [email protected], check out new blog"

I write the username/email on that page with

<?php echo $_GET['user']; ?>

Now how can I make this page to expire after you visit it? I mean I want to visit it only once time!

*EDITED

Look the code I use already to generate unique links that expire:

<?php

    include("variables.php");

    $password = trim($_SERVER['QUERY_STRING']);

    if($password == ADMIN_PASSWORD) {
        $new = uniqid('key',TRUE);

        if(!is_dir('keys')) {
            mkdir('keys');
            $file = fopen('keys/.htaccess','w');
            fwrite($file,"Order allow,deny\nDeny from all");
            fclose($file);
        }

        $file = fopen('keys/keys','a');
        fwrite($file,"{$new}\n");
        fclose($file);
?>

<html>
    <head>
        <title>Page created</title>
        <style>
            nl { 
                font-family: monospace 
            }
        </style>
    </head>
    <body>
        <h1>Page key created</h1>
        Your new single-use page link:<br>
        <nl>
        <?php 
            echo "http://" . $_SERVER['HTTP_HOST'] . DOWNLOAD_PATH . "?" . $new; 
        ?></nl>
    </body>
</html>

<?php
    } else {

        header("HTTP/1.0 404 Not Found");
    }
?>`

Also this code expire in 36 hours, and is set by variables.php

<?

    define('PROTECTED_DOWNLOAD','download.php');

    define('DOWNLOAD_PATH','/.work/page.php');

    define('SUGGESTED_FILENAME','download-doc.php');

    define('ADMIN_PASSWORD','1234');

    define('EXPIRATION_DATE', '+36 hours');

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: ".date('U', strtotime(EXPIRATION_DATE)));

?>

This php files generates a link like this: http://www.mysite.com/page.php?key1234567890 Now how to add the variable [email protected] next to uniqid to generate a link like this: page.php?key1234567890&[email protected] to can get the [email protected] by <?php echo $_GET['user']; ?> in the next page.

Sorry for my bad english.

Upvotes: 1

Views: 1913

Answers (5)

Anand Solanki
Anand Solanki

Reputation: 3425

Try this:

You can only do this with database update mechanism safely. Set database field with 0 default and make it 1 after user visit that page.

For Example,

In some website you find such options in Forget Password Link Page

Upvotes: 1

Franklin
Franklin

Reputation: 1801

If you want to avoid interacting with a database, you could also create a cookie, and then look at the cookie when someone navigates to http://www.mysite.com/newsletter.php. The only problem with this is that the page will appear again when the user clears his/her cookies.

Upvotes: 1

The Hungry Dictator
The Hungry Dictator

Reputation: 3484

Just add an extra column into your K. Which will store the status of the page. When user visite your page. Just change the status. And don't allow pages whose status is changed. You can easily do it with minor change in database query. That will remove lot of burden of maintaining using session and cookies.

Upvotes: 0

user2251804
user2251804

Reputation: 11

another option will be using client side cache for this. We can save a cookie the first time the user visits and on next visit we can check if user is visited earlier and display him message. http://plugins.jquery.com/project/Cookie

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

Store a status variable in the database, when you have a visit on that page then compare the email with the one stored in the database having 0 visits so far, update the status variable to show visited=1 and next time when same url is opened, look at the status variable and since its 1, don't show that page.

Upvotes: 4

Related Questions