生成php程序的php代码
一个.php文件,包含程序开始符(),具体代码则放置在开始符和结束符之间。为了方便日后修改和程序读取,习惯上代码应当按照一定的规定分行,而php代码必定少不了变量标记符($)。这些,都给生成.php文件带来一定的麻烦,处理它们需要一些变通方式。
我们先来处理php程序文件的开始符和结束符,这个不太复杂,我们用两个变量来表示它们:
$str_tmp="$str_end="?>"; //结束符。$str_end为独立变量。
下面来处理变量标记符($),设我们将在生成的.php文件里存在一个这样的变量等式:$age="28";。代码如下:
$str="$"."age=\"28\";\r\n";
上式里,打印变量 $str 将得到预期的效果。代码中,$和age要分开并用连接号(.)将其组合才能得到 $age 的效果;符号(\")表示小解双引号(");php代码一行结束要有分号(;);\r\n则表示打个回车另起一行。
以上我们解决了php代码起始符、结束符及代码变量标志和等式等输入问题,现在就可以具体进入生成.php文件的操作了。为了让程序看起来像个样子,我们预设一下即将生成的.php文件的内容:用户名(黑马)、登录密码(blackhorse)、年龄(28)、收入(4500)。以下是生成.php文件的完整代码:
//搜集资料
$str_tmp="$str_end="?>"; //php结束符
$str_tmp.="$"."name=\"黑马\";\r\n"; //加入用户名
$str_tmp.="$"."pass=\"blackhorse\";\r\n"; //加入密码
$str_tmp.="$"."age=\"28\";\r\n"; //加入年龄
$str_tmp.="$"."income=\"4500\";\r\n";
$str_tmp.=$str_end; //加入结束符
//保存文件
$sf="blhorse.php"; //文件名
$fp=fopen($sf,"w"); //写方式打开文件
fwrite($fp,$str_tmp); //存入内容
fclose($fp); //关闭文件
?>
把上述代码保存为一个名为 test.php 的文件,运行它,将得到 blhorse.php 文件,该文件的完整内容如下:
$name="黑马";
$pass="blackhorse";
$age="28";
$income="4500";
?>
可以看得出来,我们得到的是一个相当标准的.php配置类文件,完全符合我们的要求。至于要写更复杂的.php文件,只要像本文代码所显示的那样处理好变量的赋值和变量的累加,实现起来就变得相当容易。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.