1

我只想让初次使用的用户“浏览”我的网站。我想用 JavaScript 做。我的意思是,如果用户从未看过我的网站并且这是他/她的第一次访问,他们将获得有关如何使用该网站的教程。如果他们再次访问或重新加载页面,他们不应该看到工具提示。这可以用 JavaScript Cookies 完成吗?我找到了一个 PHP 代码,但我现在想避免使用 PHP。

<?php
// Top of the page, before sending out ANY output to the page.
    $user_is_first_timer = !isset( $_COOKIE["FirstTimer"] );

// Set the cookie so that the message doesn't show again
    setcookie( "FirstTimer", 1, strtotime( '+1 year' ) );
?>




<H1>hi!</h1><br>


<!-- Put this anywhere on your page. -->
<?php if( $user_is_first_timer ): ?>
    Hello there! you're a first time user!.
<?php endif; ?>

如果用JS做不到,可以用.htaccess做吗?我还发现了这个:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
4

1 回答 1

5

当然,虽然我更喜欢使用localStorage来存储“用户之前访问过”信息,因为如果您设置了一个 cookie,那么该 cookie 将随每个发送到服务器。单身的。要求。制成。至。您的。服务器。

所以,试试这个:

if( !window.localStorage) {
    // no localStorage (old browser) - either fall back to cookie method,
    // or just do nothing and skip the whole tour thing - if the user
    // can't be bothered to upgrade, why should you bother to accomodate them?
}
else {
    if( !window.localStorage.isReturningVisitor) {
        // do all the tour stuff here
        window.localStorage.isReturningVisitor = true;
    }
}
于 2013-01-24T21:06:00.260 回答