Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

23 practical php code snippet writing method tutorial

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "23 php practical code snippet writing method tutorial". In the daily operation, I believe that many people have doubts on the 23 php practical code snippet writing method tutorial. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the "23 php practical code snippet writing method tutorial". Next, please follow the editor to study!

1. Send SMS

When developing Web or mobile applications, you often encounter the need to send SMS to users, either for login reasons, or to send messages. The following PHP code implements the function of sending SMS.

In order to send SMS in any language, you need a SMS gateway. Most SMS will provide an API, in this case using MSG91 as the SMS gateway.

Function send_sms ($mobile,$msg) {$authKey = "XXXXXXXXXXX"; date_default_timezone_set ("Asia/Kolkata"); $date = strftime ("% Y-%m-%d% H:%M:%S"); / / Multiple mobiles numbers separated by comma$mobileNumber = $mobile; / / Sender ID,While using route4 sender id should be 6 characters long.$senderId = "IKOONK"; / / Your message to send, Add URL encoding here.$message = urlencode ($msg); / / Define route$route = "template" / / Prepare you post parameters$postData = array ('authkey' = > $authKey,' mobiles' = > $mobileNumber, 'message' = > $message,' sender' = > $senderId, 'route' = > $route); / / API URL$url= "https://control.msg91.com/sendhttp.php"; / / init the resource$ch = curl_init (); curl_setopt_array ($ch, array (CURLOPT_URL = > $url, CURLOPT_RETURNTRANSFER = > true, CURLOPT_POST = > true, CURLOPT_POSTFIELDS = > $postData / /, CURLOPT_FOLLOWLOCATION = > true) / / Ignore SSL certificate verificationcurl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); / / get response$output = curl_exec ($ch); / / Print error if anyif (curl_errno ($ch)) {echo 'error:'. Curl_error ($ch);} curl_close ($ch);}

Where "$authKey =" XXXXXXXXXXX ";" requires you to enter your password, "$senderId =" IKOONK ";" requires you to enter your SenderID. You need to specify a country code when entering a mobile number (for example, 1 in the United States and 91 in India).

Syntax:

two。 Use mandrill to send mail

Mandrill is a powerful SMTP provider. Developers tend to use a third-party SMTP provider for better pickup delivery.

In the following function, you need to put "Mandrill.php" in the same folder as a PHP file, so that you can use TA to send mail.

Function send_email ($to_email,$subject,$message1) {require_once 'Mandrill.php';$apikey =' XXXXXXXXXX'; / / specify your apikey here$mandrill = new Mandrill ($apikey); $message = new stdClass (); $message- > html = $message1;$message- > text = $message1;$message- > subject = $subject;$message- > from_email = "blog@koonk.com"; / / Sender Email$message- > from_name = "KOONK"; / / Sender Name$message- > to = array (array ("email" = > $to_email); $message- > track_opens = true $response = $mandrill- > messages- > send ($message);}

$apikey = 'XXXXXXXXXX'; / / specify your apikey here "here requires you to specify your API key (obtained from your Mandrill account).

Syntax:

For best results, it is best to follow Mandrill's tutorial to configure DNS.

3. PHP function: block SQL injection

SQL injection or SQLi common means of attacking websites, using the following code can help you prevent these tools.

Function clean ($input) {if (is_array ($input)) {foreach ($input as $key = > $val) {$output [$key] = clean ($val); / / $output [$key] = $this- > clean ($val);} else {$output = (string) $input; / / if magic quotes is on then use stripslashes if (get_magic_quotes_gpc ()) {$output = stripslashes ($output) } / / $output = strip_tags ($output); $output = htmlentities ($output, ENT_QUOTES, 'UTF-8');} / / return the clean text return $output;}

Syntax:

4. Detect user location

Use the following function to detect in which city the user is visiting your website

Function detect_city ($ip) {$default = 'UNKNOWN'; $curlopt_useragent =' Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (. NET CLR 3.5.30729)'; $url = 'http://ipinfodb.com/ip_locator.php?ip='. Urlencode ($ip); $ch = curl_init (); $curl_opt = array (CURLOPT_FOLLOWLOCATION = > 1, CURLOPT_HEADER = > 0, CURLOPT_RETURNTRANSFER = > 1, CURLOPT_USERAGENT = > $curlopt_useragent, CURLOPT_URL = > $url, CURLOPT_TIMEOUT = > 1, CURLOPT_REFERER = > 'http://'. $_ SERVER ['HTTP_HOST'],); curl_setopt_array ($ch, $curl_opt); $content = curl_exec ($ch); if (! is_null ($curl_info)) {$curl_info = curl_getinfo ($ch);} curl_close ($ch); if (preg_match (' {City: ([^)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report