Categories


Tags


21个实用PHP代码

1. 计算字符串长度: ```php $string = "Welcome to the PHP world!"; $length = strlen($string); echo $length; //输出:27 ``` 2. 字符串截取: ```php $string = "Welcome to the PHP world!"; $substring = substr($string, 0, 7); //从0开始截取,截取长度为7 echo $substring; //输出:Welcome ``` 3. 数组遍历: ```php $fruits = array("apple", "banana", "orange"); foreach($fruits as $fruit) { echo $fruit . "
"; } //输出: //apple //banana //orange ``` 4. 数组排序: ```php $numbers = array(5, 2, 8, 1, 3); sort($numbers); //升序排序 print_r($numbers); //输出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 8 ) ``` 5. 数组查找: ```php $fruits = array("apple", "banana", "orange"); $key = array_search("banana", $fruits); echo $key; //输出:1 ``` 6. 获取当前时间戳: ```php $timestamp = time(); echo $timestamp; //输出:1631771555 ``` 7. 将时间戳格式化为日期: ```php $timestamp = time(); $date = date("Y-m-d", $timestamp); echo $date; //输出:2021-09-16 ``` 8. 获取当前文件路径: ```php $path = $_SERVER['REQUEST_URI']; echo $path; //输出:/index.php ``` 9. 获取当前文件名: ```php $filename = basename($_SERVER['SCRIPT_FILENAME']); echo $filename; //输出:index.php ``` 10. 设置Cookie: ```php setcookie("username", "John"); ``` 11. 获取Cookie: ```php $username = $_COOKIE["username"]; echo $username; //输出:John ``` 12. 删除Cookie: ```php setcookie("username", "", time() - 3600); //设置过期时间为1小时前 ``` 13. 判断是否存在指定Cookie: ```php if(isset($_COOKIE["username"])) { echo "Cookie存在"; } else { echo "Cookie不存在"; } ``` 14. 获取客户端IP地址: ```php $ip = $_SERVER['REMOTE_ADDR']; echo $ip; //输出:127.0.0.1 ``` 15. 获取客户端浏览器信息: ```php $browser = $_SERVER['HTTP_USER_AGENT']; echo $browser; //例如:Mozilla/5.0 (Windows NT 10.0; … ``` 16. 发送电子邮件: ```php $to = "someone@example.com"; $subject = "Subject"; $message = "Hello! This is a test email."; $headers = "From: sender@example.com"; mail($to, $subject, $message, $headers); ``` 17. 加密字符串: ```php $password = "123456"; $encrypted_password = md5($password); echo $encrypted_password; //输出:e10adc3949ba59abbe56e057f20f883e ``` 18. 解码URL: ```php $url = "https%3A%2F%2Fwww.baidu.com%2F"; $url_decoded = urldecode($url); echo $url_decoded; //输出:https://www.baidu.com/ ``` 19. 编码URL: ```php $url = "https://www.baidu.com/"; $url_encoded = urlencode($url); echo $url_encoded; //输出:https%3A%2F%2Fwww.baidu.com%2F ``` 20. 向数据库插入数据: ```php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } mysqli_close($conn); ``` 21. 从数据库中查询数据: ```php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ```

Public @ 2023-04-01 12:51:23

phpmyadmin导入导出mysql

访问登录phpmyadmin输入相应数据库用户名及密码,如图:image.png在phpmyadmin管理中先点击相应的数据库,例如:myhost,进入到myhost数据库的管理界面以后再点击【导入】按钮。注意:mysql默认的数据库和schema库不要删除和修改,误操作将会导致wdcp管理系统损坏!!,进入phpmyadmin后点击对应的数据库进入后再导入image.png点击“选择文件”,选择

Public @ 2014-10-17 15:49:29

PHP程序提示sql注入漏洞的处理方法

可以下载PHP的防护脚本:http://downinfo.myhostadmin.net/php360.zip1.解压后将360_safe3.php传到要包含的文件的目录2.在页面中加入防护,有两种做法,根据情况二选一即可:a).在所需要防护的页面加入代码require_once('360_safe3.php');就可以做到页面防注入、跨站如果想整站防注,就在网站的一个公用文件中,

Public @ 2010-10-25 15:46:34

PHP字符串操作入门教程

PHP字符串操作指的是对字符串进行各种常见操作的方法。字符串是PHP中最常用的数据类型之一,因此了解如何操作字符串是非常重要的。 以下是一些常见的PHP字符串操作: 1. 字符串长度:使用PHP的strlen()函数可以获取字符串的长度。 ``` $string = "Hello world"; echo strlen($string); // 输出 11 ``` 2. 字符串提取:可以使

Public @ 2023-04-16 19:00:36

更多您感兴趣的搜索

0.849273s