当前位置: 编程技术>php
本页文章导读:
▪php 计数器实例(基于文件、基于cookie方式) 1,基于文件实现的php计数器
<!-- 首先创建一个空文件:myData.dat-->
<?php
$cfile = "myData.dat";
$fh = fopen($cfile, "r+");
if (!$fh){
die("<BR>Failed to open file <I>$cfile</I>.");
}
.........
▪php应用开发之文件下载详解 1,php下载函数
借助header()函数与readfile()函数实现文件下载功能。
<?php
function DownloadFile($filename)
{
// Check filename
if (empty($filename) || !file_exists($filename))
{
return FALSE;
.........
▪php 查看页面源代码的实现代码(图文) 本节分享的这段php代码,可用于显示与查看网页的源代码。
代码:
<?php
/**
* 显示与查看网页源代码
* edit:www.
*/
// Page title
$pagetitle = 'Baumeister Mediasoft Engineering :: Resources :: '
.'.........
[1]php 计数器实例(基于文件、基于cookie方式)
来源: 互联网 发布时间: 2013-12-24
1,基于文件实现的php计数器
<!-- 首先创建一个空文件:myData.dat--> <?php $cfile = "myData.dat"; $fh = fopen($cfile, "r+"); if (!$fh){ die("<BR>Failed to open file <I>$cfile</I>."); } $s = fgets($fh, 6); $count = (int) $s; $count = $count + 1; $count = str_pad($count, 6); rewind($fh); fwrite($fh, $count); echo "$count"; fclose($fh); ?>
2,基于cookie的php计数器
<?php if (!isset($_COOKIE['visits'])) $_COOKIE['visits'] = 0; $visits = $_COOKIE['visits'] + 1; setcookie('visits',$visits,time()+3600*24*365); ?> <html> <head> <title>基于cookie实现的php计数器-www.</title> </head> <body> <?php if ($visits > 1) { echo("This is visit number $visits."); } else { // First visit echo('欢迎来到,开启你的技术之旅吧!'); } ?> </body> </html>
[2]php应用开发之文件下载详解
来源: 互联网 发布时间: 2013-12-24
1,php下载函数
借助header()函数与readfile()函数实现文件下载功能。
<?php function DownloadFile($filename) { // Check filename if (empty($filename) || !file_exists($filename)) { return FALSE; } // Create download file name to be displayed to user $saveasname = basename($filename); // Send binary filetype HTTP header header('Content-Type: application/octet-stream'); // Send content-length HTTP header header('Content-Length: '.filesize($filename)); // Send content-disposition with save file name HTTP header header('Content-Disposition: attachment; filename="'.$saveasname.'"'); // Output file readfile($filename); // Done return TRUE; } ?>
2,php文件下载脚本
更完善的php下载的自定义函数。
<?php ////////////////////////////////////////////////////////////////////// // DOWNLOAD.PHP -- Download Utility // Get download file id (assume it's passed as a form or URL parameter) $id = getGlobalVar('id', 1); // Check download file id parameter, get download file name, download file // (assuming (global) variable $id specifies download file id) if (empty($id) || !DownloadFile(getDownloadFileName($id))) { die("No Download!"); } // Only functions beyond this point function getDownloadFilename($fileid) // Get download file pathname // Returns: download file pathname // Parameters: // $fileid : File identifier { // Download file list $DLFILES = array( 'TOOL1' => 'download/tool1_v30.exe', 'PROG2' => 'download/prog2setup.exe', ); // Get/check download file name if (empty($fileid) || empty($DLFILES[$fileid])) { return ''; } // Set base directory to document root directory // (could also be set to a directory outside document root!) $basedir = getGlobalVar('DOCUMENT_ROOT'); // Build and return download file name return $basedir.'/'.$DLFILES[$fileid]; } function DownloadFile($filename) // Download file // Returns: TRUE if download successfully started, FALSE if download failed // Parameters: // $filename : Download file pathname { // Verify filename if (empty($filename) || !file_exists($filename)) { return FALSE; } // Create download file name to be displayed to user $saveasname = basename($filename); // Send binary filetype HTTP header header('Content-Type: application/octet-stream'); // Send content-length HTTP header header('Content-Length: '.filesize($filename)); // Send content-disposition with save file name HTTP header header('Content-Disposition: attachment; filename="'.$saveasname.'"'); // Output file readfile($filename); // Download successfully started return TRUE; } function getGlobalVar($g, $formflag = 0) // Get global PHP variable value // Returns: global variable value or empty string if not available // Parameters: // $g : Global PHP variable name // $formflag : Flag - global var from GET/POST input { if (empty($g)) { return 0; } // Try superglobal access (PHP 4.1.0+) if ($formflag) { if (isset($_POST[$g])) { return $_POST[$g]; } if (isset($_GET[$g])) { return $_GET[$g]; } if (isset($_REQUEST[$g])) { return $_REQUEST[$g]; } } else { if (isset($_SERVER[$g])) { return $_SERVER[$g]; } if (isset($_ENV[$g])) { return $_ENV[$g]; } } // Try superglobal access (PHP 3.0.0+) if (isset($GLOBALS[$g])) { return $GLOBALS[$g]; } // Try global variable access (PHP 3+) global $$g; if (!empty($$g)) { return $$g; } // Assume global variable empty/not set return ''; } ?>
将以上脚本保存为dl.php,在应用时传入id参数即可。
例如:
<a href="http://www.YourDomain.com/dl.php?id=PROG2">Download Program 2</a>
也可以通过php的重定向语句Location来实现,例如:
<?php header("Location: http://www.YourDomain.com/dl.php?id=PROG2"); exit; ?>
以上的代码,可以防止用户直接访问下载文件,起到了一定的文件保护,甚至是防盗链的功能。
以下的代码,可以依据http头信息,作些介绍,从而提供更安全的文件下载。
代码:
<?php function DownloadFile($filename) { // Check filename if (empty($filename) || !file_exists($filename)) { return FALSE; } // Create download file name to be displayed to user $saveasname = basename($filename); // Send binary filetype HTTP header header('Content-Type: application/octet-stream'); // Send content-length HTTP header header('Content-Length: '.filesize($filename)); // Send content-disposition with save file name HTTP header // (using workaround for MSIE 5.5 SP1 / MSIE 6.01 bugs/problems) $browser = getGlobalVar('HTTP_USER_AGENT'); if (strstr('MSIE 5.5', $browser) || strstr('MSIE 6.01', $browser)) { header('Content-Disposition: filename="'.$saveasname.'"'); } else { header('Content-Disposition: attachment; filename="'.$saveasname.'"'); } // Send Content-Transfer-Encoding HTTP header // (use binary to prevent files from being encoded/messed up during transfer) header('Content-Transfer-Encoding: binary'); // Output file readfile($filename); // Done return TRUE; } function getGlobalVar($g, $formflag = 0) // Get global PHP variable value // Returns: global variable value or empty string if not available // Parameters: // $g : Global PHP variable name // $formflag : Flag - global var from GET/POST input { if (empty($g)) { return 0; } // Try superglobal access (PHP 4.1.0+) if ($formflag) { if (isset($_GET[$g])) { return $_GET[$g]; } if (isset($_POST[$g])) { return $_POST[$g]; } if (isset($_REQUEST[$g])) { return $_REQUEST[$g]; } } else { if (isset($_SERVER[$g])) { return $_SERVER[$g]; } if (isset($_ENV[$g])) { return $_ENV[$g]; } } // Try superglobal access (PHP 3.0.0+) if (isset($GLOBALS[$g])) { return $GLOBALS[$g]; } // Try global variable access (PHP 3+) global $$g; if (!empty($$g)) { return $$g; } // Assume global variable empty/not set return ''; } ?> <?php function DownloadFile($filename) { // Check filename if (empty($filename) || !file_exists($filename)) { return FALSE; } // Create download file name to be displayed to user $saveasname = basename($filename); // Send binary filetype HTTP header header('Content-Type: application/octet-stream'); // Send content-length HTTP header header('Content-Length: '.filesize($filename)); // Send content-disposition with save file name HTTP header // (using workaround for MSIE 5.5 SP1 / MSIE 6.01 bugs/problems) $browser = getGlobalVar('HTTP_USER_AGENT'); if (strstr('MSIE 5.5', $browser) || strstr('MSIE 6.01', $browser)) { header('Content-Disposition: filename="'.$saveasname.'"'); } else { header('Content-Disposition: attachment; filename="'.$saveasname.'"'); } // Send Content-Transfer-Encoding HTTP header // (use binary to prevent files from being encoded/messed up during transfer) header('Content-Transfer-Encoding: binary'); // Output file readfile($filename); // Done return TRUE; } function getGlobalVar($g, $formflag = 0) // Get global PHP variable value // Returns: global variable value or empty string if not available // Parameters: // $g : Global PHP variable name // $formflag : Flag - global var from GET/POST input { if (empty($g)) { return 0; } // Try superglobal access (PHP 4.1.0+) if ($formflag) { if (isset($_GET[$g])) { return $_GET[$g]; } if (isset($_POST[$g])) { return $_POST[$g]; } if (isset($_REQUEST[$g])) { return $_REQUEST[$g]; } } else { if (isset($_SERVER[$g])) { return $_SERVER[$g]; } if (isset($_ENV[$g])) { return $_ENV[$g]; } } // Try superglobal access (PHP 3.0.0+) if (isset($GLOBALS[$g])) { return $GLOBALS[$g]; } // Try global variable access (PHP 3+) global $$g; if (!empty($$g)) { return $$g; } // Assume global variable empty/not set return ''; } ?>
[3]php 查看页面源代码的实现代码(图文)
来源: 互联网 发布时间: 2013-12-24
本节分享的这段php代码,可用于显示与查看网页的源代码。
代码:
<?php /** * 显示与查看网页源代码 * edit:www. */ // Page title $pagetitle = 'Baumeister Mediasoft Engineering :: Resources :: ' .'PHP Application: Display/View Web Page Contents/Source' ; // Messages $fmturl = '<p >"%s" contents/source:</p>'."\n"; $nosource = '<span >* empty / not found *</span>'; // Form parameters $url = isset($_REQUEST['url']) ? $_REQUEST['url'] : ''; $dowrap = isset($_REQUEST['wrapsource']) && !empty($_REQUEST['wrapsource']) && ($_REQUEST['wrapsource'] == 'on') ? 1 : 0; ?> <html> <head> <title><?php echo $pagetitle;?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body > <h3 ><?php echo $pagetitle;?></h3> <hr /> <form method="post" > URL <input type="text" size="50" name="url" value="<?php echo empty($url) ? 'http://' : $url;?>" /> <input type="checkbox" name="wrapsource"<?php if ($dowrap) { echo ' checked="checked"';}?> />Wrap <input type="submit" value="Go" /> </form> <?php if (!empty($url)) { // Start web page output echo '<hr />'."\n"; // Display selected URL echo sprintf($fmturl, $url); // Enable URL-aware fopen wrappers to allow for URL file reading if ((double)phpversion() >= 4.2) { ini_set('allow_url_fopen', '1'); } // Read file $s = @file_get_contents($url); if (empty($s)) { // Web page empty/access failure echo $nosource; } else { // Display web page contents/source using form/textarea ?> <form name="_webpagesource_" > <script type="text/javascript" language="JavaScript"> <!-- // Display select all button document.write('<input type="button" value="Select All"' + ' onclick="document.forms[\'_webpagesource_\'][\'_src_\'].select();"' + ' />' ); //--> </script> <table width="100%" height="80%" border="0" cellspacing="0" cellpadding="0"><tr> <td > <textarea id="_src_" wrap="<?php echo $dowrap ? 'virtual' : 'off';?>"> <?php echo htmlspecialchars($s);?> </textarea> </td> </tr></table> </form> <?php } } ?> <hr /> <p > Copyright © 2013-<?php echo date('Y');?> by <a href="http://www." target="_top">,欢迎您。</a> </p> </body> </html>
调用示例:
最新技术文章: