mui框架-01-介绍_如何显示介绍性网页-使用PHP一次

mui框架-01-介绍

Introduction

介绍

A frequent question goes something like this, "How can I show an introductory page to my clients on the first site visit, but not show it again on every visit?"  The answer is by using a cookie.  This article shows the design pattern for a home page that is aware of the need or lack of need for an introductory "splash" page.  It also allows the client to see the splash page on demand.

常见的问题是这样的:“如何在第一次访问现场时向客户显示介绍性页面,而在每次访问时都不再显示?” 答案是通过使用cookie。 本文显示了主页的设计模式,该主页已了解介绍“闪屏”页面的需求。 它还允许客户按需查看启动页面。

Using Cookies to Preserve "Stateful" Information

使用Cookies保留“状态”信息

In HTTP, cookies are sent from the browser to the server at the time of the page request.  PHP puts these cookies into the $_COOKIE associative array.  See: http://php.net/manual/en/reserved.variables.cookies.php.  There is an important characteristic to this data flow -- the cookies you set in your PHP script are NOT put into $_COOKIE by PHP.  This means that we can test the contents of $_COOKIE to see the historical record of the cookies, and that record will not change during the execution of our script.  Cookies we set in our current script are only presented to future scripts.

在HTTP中,Cookie在页面请求时从浏览器发送到服务器。 PHP将这些cookie放入$ _COOKIE关联数组中。 请参阅: http//php.net/manual/en/r eserved.va riables.co okies.php 。 该数据流有一个重要特征-PHP不会将您在PHP脚本中设置的cookie放入$ _COOKIE。 这意味着我们可以测试$ _COOKIE的内容以查看cookie的历史记录,并且该记录在执行脚本期间不会更改。 我们在当前脚本中设置的Cookie只会显示给以后的脚本。

The setcookie() function is used to set cookies on the client browser.  See http://www.php.net/manual/en/function.setcookie.php for the details.  Cookies are part of the HTTP headers.  This means that you may only call setcookie() before any browser output has been sent, including whitespace.  There are no exceptions to this rule.

setcookie()函数用于在客户端浏览器上设置cookie。 参见http://www.php.net/manual/ zh /功能 setcooki 有关详细信息,请参见e.php 。 Cookies是HTTP标头的一部分。 这意味着您只能在发送任何浏览器输出(包括空格)之前调用setcookie()。 这条规定没有例外。

In this script we do the following things.

在此脚本中,我们执行以下操作。

(1) Unconditionally set a cookie named "splash_page" on the client browser

(1)在客户端浏览器上无条件设置名为“ splash_page”的cookie

(2) See if the client asked to see the splash page and redirect to display the splash page if it was requested

(2)查看客户端是否要求查看初始页面,并在请求时重定向到显示初始页面

(3) Decide whether to show the splash page (based on the cookies) or the regular page, and take the appropriate action

(3)确定是显示启动页面(基于cookie)还是常规页面,并采取适当的措施

Another design pattern that would work about the same way would be to have the splash page put the cookies on the browser.  In either case, our tests would ask the same question, to wit, "Has the splash page been requested?" and "Does the $_COOKIE array show that the splash page has already been shown?"

另一种将以相同方式工作的设计模式是使启动页面将cookie放置在浏览器上。 无论哪种情况,我们的测试都会问相同的问题,例如“是否请求了初始页面?” 和“ $ _COOKIE数组是否显示初始页面已显示?”

<?php // RAY_splash_page.php


// DEMONSTRATE HOW TO SHOW A SPLASH INTRODUCTION PAGE ONCE, BUT NOT REPEATEDLY
// MAN PAGE: http://us.php.net/manual/en/function.setcookie.php
// TO SEE COOKIES IN FIREFOX, FOLLOW TOOLS => OPTIONS => PRIVACY => SHOW COOKIES


// THE COOKIE LIFE WILL CAUSE THE SPLASH PAGE TO BE SHOWN NO MORE FREQUENTLY THAN ONCE A MONTH
define('COOKIE_LIFE', 60*60*24*30); // A MONTH IN SECONDS

// CHOOSE THE COOKIE NAME AND VALUE
$cookie_name  = 'splash_page';
$cookie_value = TRUE;

// USE THIS TO MAKE A PERSISTENT COOKIE - DEFINE COOKIE_LIFE IN SECONDS - date('Z') IS UTC OFFSET IN SECONDS
$cookie_expires = time() + date('Z') + COOKIE_LIFE;

// MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS IN THE WWW ROOT
$cookie_path = '/';

// MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
$x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
$y = count($x);
if ($y == 1) // MAYBE 'localhost'?
{
    $cookie_domain = $x[0];
}
else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
{
    // USE THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN
    $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
}

// MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
$cookie_secure    = FALSE;

// HIDE COOKIE FROM JAVASCRIPT (PHP 5.2+)
$cookie_http      = TRUE;

// SET THE COOKIE (IGNORE TESTING FOR SUCCESS)
setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http);


// AT THIS POINT, THE NEW COOKIE HAS BEEN SET, BUT IT IS _NOT_ AVAILABLE TO THIS SCRIPT.
// THE ONLY THING IN THE $_COOKIE ARRAY ARE THE OLD COOKIES, SO WE CAN TEST TO SEE IF THE
// SPLASH PAGE HAS BEEN SHOWN, AND IF NOT, WE CAN SHOW IT.  WE CAN ALSO GIVE THE CLIENT
// THE OPTION TO SEE THE SPLASH PAGE ON DEMAND BY PUTTING "intro=TRUE" INTO THE URL.


// IF THE CLIENT WANTS TO SEE THE SPLASH PAGE
if ( (isset($_GET["intro"])) && ($_GET["intro"] == 'TRUE') )
{
    header("Location: path/to/splash_page.php");
    exit;
}

// IF THE CLIENT HAS NOT SEEN THE SPLASH PAGE
if (!isset($_COOKIE["splash_page"]))
{
    header("Location: path/to/splash_page.php");
    exit;
}

// IF THE COOKIE WAS FOUND IN $_COOKIE, THE CLIENT HAS SEEN THE SPLASH PAGE
// PRODUCE THE HTML FOR THIS PAGE HERE...

Summary

摘要

This article shows how to detect a "first visit" to a web site.  It teaches a design that allows you to make a special welcome message to new visitors.

本文介绍如何检测对网站的“首次访问”。 它讲授一种设计,使您可以向新访客发出特别的欢迎信息。

Further Reading

进一步阅读

It may be helpful to understand the HTTP protocols and the workings of Client/Server architectural patterns.  For short-term storage, you can sometimes use the PHP Session.

了解HTTP协议和客户端/服务器体系结构模式的工作原理可能会有所帮助。 对于短期存储,有时可以使用PHP Session

Please give us your feedback!

请给我们您的反馈意见!

If you found this article helpful, please click the "thumb's up" button below. Doing so lets the E-E community know what is valuable for E-E members and helps provide direction for future articles.  If you have questions or comments, please add them.  Thanks!

如果您发现本文有帮助,请单击下面的“竖起大拇指”按钮。 这样做可以使EE社区了解对EE成员有价值的内容,并为将来的文章提供指导。 如果您有任何问题或意见,请添加。 谢谢!

翻译自: https://www.experts-exchange.com/articles/3314/How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html

mui框架-01-介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值