search
HomeBackend DevelopmentPHP TutorialPHP中的表单应用释疑_PHP

PHP中的表单应用释疑_PHP

Jun 01, 2016 pm 12:31 PM
headapplicationussubmitdocument

综述:表单作为用户提交信息的一个关键途径,一直是PHP编程中的一个最基本的方面,也是入门者会遇到的一个大的重点与难点。我们选择有关处理关联数、获得同名checkbox的选取值、上传文件方面比较容易令众困惑的地方进行归

如何使用表单传递关联数组?

通过表单传递的关联数组能被 each()函数读取,程序如下:

//test1.php
<form action="test2.php" method=post>
<input type=hidden name="var[Address]" value="Beijing">
<input type=hidden name="var['age']" value="20">
<input type=submit value=submit>
这个名为var[Address]值为"Beijing"的元素递交到test2.php后,就成了一个关联数组,var["Address"]="Beijing":
//test2.php
<?
echo $var["Address"];
?>

输出结果为:Beijing

如何处理同名checkbox?

具体代码:

test1.php:
<FORM METHOD=POST ACTION="test2.php">
苹果<INPUT TYPE="checkbox" NAME="come[]" VALUE="苹果"><BR>
鸭梨<INPUT TYPE="checkbox" NAME="come[]" VALUE="鸭梨"><BR>
香蕉<INPUT TYPE="checkbox" NAME="come[]" VALUE="香蕉"><BR>
西瓜<INPUT TYPE="checkbox" NAME="come[]" VALUE="西瓜"><BR>
<INPUT TYPE="submit" VALUE="提交">
</FORM>

test2.php:
你的选择:<BR>
<?
for ($i=0;$i<sizeof($come);$i ) echo $come[$i],"<BR>";
?>
这样从test1.php提交过来的所有名为come[]的元素就组成了一个数组,这样我们就可以很容易地处理了.

怎样才能察看提交的所有信息?

一般来说,PHP引擎将每一个表单域放到一个叫做$HTTP_POST_VARS的数组中,所以我们可以通过读取这个数组就可以察看提交的所有信息:

<?
echo "POST 所送出的值为:<BR>";
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
 echo "$key => $val<BR>";
}
?>

如何同时上传多个文件?

我们来看一个例子。

下面是上传文件的提交页面,利用该页面你不仅可以生成 1000 个上传文件框(也可以是任意多个 0~n ),而且可分别指出它们的保存路径。

提交页面的文件输入框为命名为: file0,file1,...file100,...fileN
提交页面的文件路径框为命名为: path0,path1,...path100,...pathN
由于页面的生成非常简单,所以就不在此多解释了,用 javascript 定义了两个函数,check() 用于提交页面,create()用于生成文件上传框。
phpfileup.htm
--------------------------------------------------------
【文件php9.txt】
--------------------------------------------------------
文件提交页面既已生成,下面任务就很明确了:将提交的文件内容保存到服务器上。

我们先定义一个文件保存函数 fup() 它有两个参数:
$filename: 文件内容
$fname: 文件名(包含路径)
剩下的就是写一个循环将文件依次写入服务器。

PHP 对于上传文件的处理是这样的:如果提交的文件框名为 file0, 那么提交给 PHP 的文件内容保存在变量 $file0 中,而文件名则保存在 $file0_name 中。这样在这个循环中我们要做的就是将提交页面提交的内容分解出来,实现过程请看下面的代码。

fileup.php
<<?
function fup($Local_file_name,$Remote_file_name) {
If($Local_file_name != "none") {
copy($Local_file_name,$Remote_file_name);
unlink($Local_file_name);
}
}

for($i=0;$i<$cnt;$i ) {
$ffnn="file".$i;
$ffnnname=$ffnn."_name";
$ffpath="path".$i;

//print $$ffnn;
print $$ffnnname;
print "<BR>";

fup($$ffnn,$$ffpath.$$ffnnname); //"../www/test/tmp/"
}
?>
如何对页面进行抓取和分析?

  首先,必须决定我们将抓取的URL地址。可以通过在脚本中设定或通过$QUERY_STRING传递。为了简单起见,让我们将变量直接设在脚本中。

<?
$url = 'http://www.domain.com';
?>

  第二步,我们抓取指定文件,并且通过file()函数将它存在一个数组里。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
?>

  好了,现在数组里已经有了文件了。但是,我们想分析的文本可能不全在一行里面。为了解决这个文件,我们可以简单地将数组$lines_array转化成一个字符串。我们可以使用implode(x,y)函数来实现它。如果在后面你想用explode(将字符串变量数组),将x设成"|"或"!"或其它类似的分隔符可能会更好。但是出于我们的目的,最好将x设成空格。y是另一个必要的参数,因为它是你想用implode()处理的数组。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
?>

  现在,抓取工作就做完了,下面该进行分析了。在这个例子中,我们想得到在<head>到</head>之间的所有东西。为了分析出字符串,我们还需要正规表达式。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
eregi("<head>(.*)</head>", $lines_string, $head);
?>

  在上面的代码中,eregi()函数按下面的格式执行:

eregi("<head>(.*)</head>", $lines_string, $head);
  "(.*)"表示所有东西,代表在<head>和</head>间的所有东西。

$lines_string是我们正在分析的字符串,$head是分析后的结果存放的数组。

  最后,我们可以输出数据。因为仅在<head>和</head>间存在一个实例,我们可以安全的假设数组中仅存在着一个元素,而且就是我们想要的。

<?
$url = 'http://www.domain.com';
$lines_array = file($url);
$lines_string = implode('', $lines_array);
eregi("<head>(.*)</head>", $lines_string, $head);
echo $head[0];
?>
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software