国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 PHP的其他功能

PHP的其他功能

Jun 08, 2016 pm 05:32 PM
file gt lt php quot

<script>ec(2);</script>

5. 其他雜項(xiàng)
5.1 生成圖像

PHP可以操作處理圖像。假如你已經(jīng)安裝了GD庫(kù),你甚至可以利用PHP生成圖像。

Header("Content-type: image/gif");
$string=implode($argv," ");
$im = imagecreatefromgif("images/button1.gif");
$orange = ImageColorAllocate($im, 220, 210, 60);
$px = (imagesx($im)-7.5*strlen($string))/2;
ImageString($im,3,$px,9,$string,$orange);
ImageGif($im);
ImageDestroy($im);
?>
(譯者注:以上代碼段缺少注釋,請(qǐng)讀者參考PHP Manual的圖像處理函數(shù)部分)
這段代碼在其他頁(yè)面中通過(guò)以下標(biāo)記PHP的其他功能調(diào)用,然后以上的那段button.php3代碼取得text值并在另外取得的圖像文件中加上該值--在以上的代碼中該圖像文件是images/button1.gif--最后輸出到瀏覽器。假如你想在表單域中使用圖像按鈕,但是又不希望在每次按鈕上的文字改變后不得不重新生成新的圖像,就可以利用這樣簡(jiǎn)單的方法動(dòng)態(tài)生成圖像文件。

5.2 Cookies

PHP支持基于HTTP的cookies。在需要時(shí)你可以像使用一般變量一樣方便的使用cookie。Cookies是瀏覽器保存于客戶端的一些信息片段,由此你可以知道是否一臺(tái)特定PC上的任何人都訪問(wèn)過(guò)你的站點(diǎn),瀏覽者者在你的站點(diǎn)上的蹤跡等等。使用cookies的典型例子就是對(duì)瀏覽者偏好的甄別。Cookies由函數(shù)setcookie()設(shè)定。與輸出HTTP標(biāo)頭的函數(shù)header()一樣,setcookie()必須在任何實(shí)際內(nèi)容杯輸出到瀏覽器之前調(diào)用。以下是一個(gè)簡(jiǎn)單例子:

if (empty($VisitedBefore))
{
// 假如沒(méi)有設(shè)定cookie,為cookie賦上當(dāng)前時(shí)間值
// 函數(shù)中的最后一個(gè)參數(shù)聲明了該cookie保存的時(shí)間
// 在這個(gè)例子中是1年
// time()函數(shù)返回自1970年1月1日以來(lái)的以秒數(shù)計(jì)的時(shí)間
SetCookie("VisitedBefore",time(), time() (60*60*24*365));
}
else
{
// 歡迎瀏覽者再次光臨
echo "Hello there, welcome back
";
// 讀取cookie并判定
if ( (time() - $VisitedBefore) >= "(60*60*24*7)" )
echo "Why did you take a week to come back. You should be here more often!? ";
}
?>

5.3 基于HTTP驗(yàn)證

基于HTTP驗(yàn)證當(dāng)PHP以CGI模式運(yùn)行時(shí)不能實(shí)現(xiàn)。我們可以使用函數(shù)header()發(fā)送HTTP標(biāo)頭強(qiáng)制驗(yàn)證,客戶端瀏覽器則彈出供輸入用戶名和密碼的對(duì)話框。這兩個(gè)變量被儲(chǔ)存在$PHP_AUTH_USER和$PHP_AUTH_PW中,你可以使用這兩個(gè)變量驗(yàn)證合法并答應(yīng)進(jìn)入。以下的例子通過(guò)用戶名稱/密碼對(duì)為tnc/nature的驗(yàn)證一名用戶的登錄:

if(!isset($PHP_AUTH_USER))
{
Header("WWW-Authenticate: Basic realm="My Realm"");
Header("HTTP/1.0 401 Unauthorized");
echo "Text to send if user hits Cancel button ";
exit;
}
else
{
if ( !($PHP_AUTH_USER=="tnc" && $PHP_AUTH_PW=="nature") )
{
// 假如是錯(cuò)誤的用戶名稱/密碼對(duì),強(qiáng)制再驗(yàn)證
Header("WWW-Authenticate: Basic realm="My Realm"");
Header("HTTP/1.0 401 Unauthorized");
echo "ERROR : $PHP_AUTH_USER/$PHP_AUTH_PW is invalid.";
exit;
}
else
{
echo "Welcome tnc!";
}
?>
事實(shí)上再實(shí)際引用中不大可能如上面使用代碼段明顯的用戶名稱/密碼對(duì),而是利用數(shù)據(jù)庫(kù)或者加密的密碼文件存取它們。

5.4 文件上傳

你可以利用PHP實(shí)現(xiàn)文件的功能,注重客戶端的瀏覽器應(yīng)該是Netscape3以上或者IE3以上。以下就是該功能的簡(jiǎn)單演示:
( upload.html ):


Upload Your File


ENCTYPE="multipart/form-data" METHOD=POST>
NAME="MAX_FILE_SIZE" VALUE="2000000">
NAME="uploadfile" SIZE="24" MAXLENGTH="80">



NAME="sendit">
NAME="cancelit">


(You may notice a slight
delay while we upload your file.)




下面是處理上傳的文件:
( receiver.php3 ):

function do_upload ()
{
global $uploadfile, $uploadfile_size;
global $local_file, $error_msg;
if ( $uploadfile == "none" )
{
$error_msg = "You did not specify a file for uploading.";
return;
}
if ( $uploadfile_size > 2000000 )
{
$error_msg = "Sorry, your file is too large.";
return;
}
$the_time = time ();
// 你需要對(duì)以下目錄有寫權(quán)限
$upload_dir = "/local/uploads";
$local_file = "$upload_dir/$the_time";
if ( file_exists ( '$local_file' ) )
{
$seq = 1;
while ( file_exists ( "$upload_dir/$the_time$seq" ) ) { $seq ; }
$local_file = "$upload_dir/$the_time$seq";
};
rename ( $uploadfile, $local_file );
display_page ();
}
function display_page ()
{
// 這里是你的頁(yè)面內(nèi)容
}


php3 Receiving Script



if ( $error_msg ) { echo "$error_msg

"; }
if ( $sendit )
{
do_upload ();
}
elseif ( $cancelit )
{
header ( "Location: $some_other_script" );
exit;
}
else
{
some_other_func ();
}
?>



5.5 常用函數(shù)

我們簡(jiǎn)單來(lái)看看一些常用的函數(shù)。

數(shù)組


array - 生成數(shù)組
count - 數(shù)組元素個(gè)數(shù)
sort - 數(shù)組排序,另有其他幾種排序函數(shù)可供使用
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to upgrade PHP version? How to upgrade PHP version? Jun 27, 2025 am 02:14 AM

Upgrading the PHP version is actually not difficult, but the key lies in the operation steps and precautions. The following are the specific methods: 1. Confirm the current PHP version and running environment, use the command line or phpinfo.php file to view; 2. Select the suitable new version and install it. It is recommended to install it with 8.2 or 8.1. Linux users use package manager, and macOS users use Homebrew; 3. Migrate configuration files and extensions, update php.ini and install necessary extensions; 4. Test whether the website is running normally, check the error log to ensure that there is no compatibility problem. Follow these steps and you can successfully complete the upgrade in most situations.

How do I prevent cross-site request forgery (CSRF) attacks in PHP? How do I prevent cross-site request forgery (CSRF) attacks in PHP? Jun 28, 2025 am 02:25 AM

TopreventCSRFattacksinPHP,implementanti-CSRFtokens.1)Generateandstoresecuretokensusingrandom_bytes()orbin2hex(random_bytes(32)),savethemin$_SESSION,andincludetheminformsashiddeninputs.2)ValidatetokensonsubmissionbystrictlycomparingthePOSTtokenwiththe

PHP beginner guide: Detailed explanation of local environment configuration PHP beginner guide: Detailed explanation of local environment configuration Jun 27, 2025 am 02:09 AM

To set up a PHP development environment, you need to select the appropriate tools and install the configuration correctly. ①The most basic PHP local environment requires three components: the web server (Apache or Nginx), the PHP itself and the database (such as MySQL/MariaDB); ② It is recommended that beginners use integration packages such as XAMPP or MAMP, which simplify the installation process. XAMPP is suitable for Windows and macOS. After installation, the project files are placed in the htdocs directory and accessed through localhost; ③MAMP is suitable for Mac users and supports convenient switching of PHP versions, but the free version has limited functions; ④ Advanced users can manually install them by Homebrew, in macOS/Linux systems

How to combine two php arrays unique values? How to combine two php arrays unique values? Jul 02, 2025 pm 05:18 PM

To merge two PHP arrays and keep unique values, there are two main methods. 1. For index arrays or only deduplication, use array_merge and array_unique combinations: first merge array_merge($array1,$array2) and then use array_unique() to deduplicate them to finally get a new array containing all unique values; 2. For associative arrays and want to retain key-value pairs in the first array, use the operator: $result=$array1 $array2, which will ensure that the keys in the first array will not be overwritten by the second array. These two methods are applicable to different scenarios, depending on whether the key name is retained or only the focus is on

How to use php exit function? How to use php exit function? Jul 03, 2025 am 02:15 AM

exit() is a function in PHP that is used to terminate script execution immediately. Common uses include: 1. Terminate the script in advance when an exception is detected, such as the file does not exist or verification fails; 2. Output intermediate results during debugging and stop execution; 3. Call exit() after redirecting in conjunction with header() to prevent subsequent code execution; In addition, exit() can accept string parameters as output content or integers as status code, and its alias is die().

Applying Semantic Structure with article, section, and aside in HTML Applying Semantic Structure with article, section, and aside in HTML Jul 05, 2025 am 02:03 AM

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

How do I access session data in PHP? How do I access session data in PHP? Jun 30, 2025 am 01:33 AM

To access session data in PHP, you must first start the session and then operate through the $_SESSION hyperglobal array. 1. The session must be started using session_start(), and the function must be called before any output; 2. When accessing session data, check whether the key exists. You can use isset($_SESSION['key']) or array_key_exists('key',$_SESSION); 3. Set or update session variables only need to assign values ??to the $_SESSION array without manually saving; 4. Clear specific data with unset($_SESSION['key']), clear all data and set $_SESSION to an empty array.

What are recursive functions in PHP? What are recursive functions in PHP? Jun 29, 2025 am 02:02 AM

Recursive functions refer to self-call functions in PHP. The core elements are 1. Defining the termination conditions (base examples), 2. Decomposing the problem and calling itself recursively (recursive examples). It is suitable for dealing with hierarchical structures, disassembling duplicate subproblems, or improving code readability, such as calculating factorials, traversing directories, etc. However, it is necessary to pay attention to the risks of memory consumption and stack overflow. When writing, the exit conditions should be clarified, the basic examples should be gradually approached, the redundant parameters should be avoided, and small inputs should be tested. For example, when scanning a directory, the function encounters a subdirectory and calls itself recursively until all levels are traversed.

See all articles