首页 > 编程技术 > php

PHP生成动态WAP页面

发布时间:2016-11-25 16:32

WAP(无线通讯协议)是在数字移动电话、个人手持设备(PDA等)及计算机之间进行通讯的开放性全球标准。由于静态的WAP页面在很多方面不能满足用户个性化的服务请求,因此通过WAP服务器端语言产生动态的WML页面,具有很广泛的应用价值和很高的商业价值。
  WAP应用结构非常类似于Internet,一个典型的WAP应用请求是这样的:首先,具有WAP用户代理功能的移动终端(WAP手机等)通过内部运行的微浏览器(Micro Browser)对某一网站以无线方式发送WAP服务请求。该请求先由WAP网关截获,对信息内容进行编码压缩,以减少网络数据流量,同时根据需要将WAP协议转换成HTTP协议,然后将处理后的请求转送到相应WAP服务器。在WAP服务器端,根据页面扩展名等性质,被请求的页面直接或由服务器端脚本解释后输出,再经网关传回用户。
  从上述WAP应用流程可以看到,生成动态WAP页面与动态产生Web网页的过程非常类似。但是由于WAP应用使用的WML语言来源于语法严格的XML,因此要求输出的格式必须按WAP网页的规范输出。同时,由于WAP协议的应用范围、移动客户端的软硬件水平等特殊性,对每次输出的页面的大小、图像的格式及容量都有一定限制。下面我们以PHP脚本语言为例,看看如何动态输出WAP页面。
  一、设置WEB服务器
  首先你的 Web服务器要安装好PHP,即能处理PHP脚本程序。其次,为使Web服务器能同时识别和处理PHP、WML、WBMP等文件,Web 服务器的MIME表需添加以下的几种文件类型。
  text/vnd.wap.wml .wml
  image/vnd.wap.wbmp .wbmp
  application/vnd.wap.wmlc .wmlc
  text/vnd.wap.wmls.wmls
  application/vnd.wap.wmlsc .wmlsc
  二、用PHP输出简单动态WAP页面
  下面有一个最简单的PHP生成WAP页面的例子。注意由于需要PHP解释器来解释该程序,并输出WAP页面,因此所有类似程序应以.php为扩展名。
  〈?php
  header(″Content-type: text/vnd.wap.wml″);
  echo (″〈wml〉 〈card〉 〈p〉″);
  echo date( ″l dS of F Y h:i:s A″ );
  echo (″〈/p〉〈/card〉〈/wml〉″);
  ?〉
  该例子在WAP手机模拟器中可以浏览,输出当前日期时间,而在普通的浏览器中无法识别,甚至会被认为是错误下载。这是因为在程序开头就声明了该输出文档为WML类型,该类型只有WAP设备能够识别并解释。值得注意的是,我们常见的HTML语言对规范性要求不严,大多数浏览器能“容忍”其中相当多的编写错误,而WML规范相当严格,一点失误都可能导致无法输出所需页面。
一、session概述 
      session是什么,刚开始我也不明白,非专业词典翻译为会议,会议期。作个不太恰当的比喻吧
(虽然不恰当,但意义却是一样的),session是你和网站之间的感情。
session在WEB技术中占有非常重要的份量。由于网页是一种无状态的连接程序,因此你无法得知用户的浏览状态。因此我们必须 
通过session记录用户的有关信息,以供用户再次以此身份对web服务器提供要求时作确认,例
如,我们在某些网站中常常要求用户登录, 但我们怎么知道用户已经登录了呢,如果没有session的话,登录信息是无法保留的,那岂不要让用户在每一页网页中都要提供用户名和密码。
 
当然,session不光用于用户身份认证功能,还可能用于其它方面,以后我们会提到的。
session用中文来解释就是会话期。一个会话期开始于用户输入一个站点的网址时,结束于他离开这个站点时。session最早出现在动态脚本语言Active Server Pages中,它的功能之强大,是一句话无法说清楚的。

 
当php还在3.0版本时,session是它永远的痛。虽然php具有执行速度快,使用灵活,功能强大等优点,但因为session的问题,使很多站点的开发放弃了php,至少我的老板是这样认为的。当时有很多php免费函数库提供在php3上实现session的方案,但都让人感觉不正宗。就好象你花好几千大洋买的手机却配置一个很粗糙的草作的袋子一样,虽然功能是一样的,但总让人觉得别扭。php4的出现让php在session问题上有了翻身的机会。虽然它的session实现还不是很理想(主要是效率问题),但毕竟是它自己实现的,而且可以实际使用了。
那我们用session干什么呢,你说了半天,我用不上的话,你岂不有卖纸张之嫌。OK,我们来看看session有什么用:作过网站的人都有这样的体会,在一页页面中的变量(在本章都指服务器端变量,下同)是不能在下一页中用的,虽然有一些办法可以实现,比如用form,urlstring等等,但有些对于用户来说是不方便的,即使让form自动提交,但其中的延时在现今的网络状况下足以让人窒息,而这两种方法都明显加大程序员的负担。如果你正在开发一个大型项目,那这些额外的负担是不能忽略的。而有了session就好办了,session中注册的变量可以作为全局变量使用。什么,全局变量? 好极了。这样一来,你知道有什么用了吧:最主要的用于用户身份认证,程序状态记录,页面之间参数传递。
比如我有0.10456,要变成10.5%?
round
(PHP 3, PHP 4 )
round -- 对浮点数进行四舍五入
说明
float round ( float val [, int precision])
返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。
注意
PHP 默认不能正确处理类似 "12,300.2" 的字符串。见字符串转换为数值。
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
注: precision 参数仅在 PHP 4 中可用。
echo round(0.10456*100,1);
<?php
$num = 0.10456;
echo round(($num*100),1)."%";
?>
Attributed to Solomon W. Golomb; a method for swapping the values of two integer variables without using an intermediate variable (you can tell this dates from the Elder Days, when variables were expensive!). Thanks to PHP's syntax it's also a one-liner.
$a^=$b^=$a^=$b;
Okay, here's how it goes (yeah, like I need to make content-free posts just for the sake of an increment...).
First, simplify the line; noting that ^= is right-associative, which means that in that line the rightmost operator is evaluated first, that the assignment operators also return the value that they assign to their lvalue, and that foo^=bar is shorthand for foo=foo^bar:

Code:

$a^=$b^=$a^=$b;
$a^=($b^=($a^=$b));
$a=$a^b;
$a^=($b^=$a);
$a=$a^$b;
$b=$b^$a;
$a=$a^$b;

Recall what ^ does. Takes each pair of corresponding bits from its arguments (the internal binary representation of its arguments, that is), and xors them together to produce the corresponding bit of the result ("corresponding" means that the first bits of both arguments produce the first bit of the result, the second bits of both arguments produce the second bit of the result, and so on. This is why, as BuzzLY noted, it's important that both variables are the same size - demons probably start flying out of your nose if one of them runs out of bits to xor before the other. So to figure out what ^ does to a pair of variables, we only need to recap what it does to single bits


session通常放在/tmp目录下,而该文件夹的权限是everbody可读,这个就非常可怕了!学校的论坛曾经就有人通过session来盗取帐号!所以后来就尝试把session放入数据库,表的结构和过程如下:
//创建表
//create sesslib.sql
CREATE TABLE sesslib (
    data text,
    time datetime,
    id int(11) DEFAULT '0' NOT NULL auto_increment,
    sid varchar(32) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE sid (sid)
);
//End
//XX.php自定义了session的数据库路径,当某个页面需要使用//session时,可以include这个部分,使用方法为:
<?
include "XX.php";//XX.php
session_start();
//以下就可以正常使用session了
?>
/******************************************************/
    XX.php 内容:
/*****************************************************/
<?
$sess_dbh="";
$sess_maxlifetime=get_cfg_var("session.gc_maxlifetime");
function sess_open($save_path, $session_name) {
    global $hostname, $dbusername, $dbpassword, $dbname, $sess_dbh;
    
    //$sess_dbh=mysql_pconnect($hostname,$dbusername,$dbpassword) or die("不能连接数据库!");
    $sess_dbh=mysql_pconnect('localhost','test','test') or die("不能连接数据库!");
    // mysql_select_db("$dbname") or die("不能选择数据库!");
mysql_select_db('test') or die("不能选择数据库!");
    return(true);
}
function sess_close() {
    //mysql_close();
    return(true);
}
function sess_read($sid) {
    global $sess_dbh;
    $result = mysql_query("select data from sesslib where sid='$sid'", $sess_dbh);
标签:[!--infotagslink--]