PHP mobile phone number home location query program code

Source: Internet
Author: User
Tags ereg

This article summarizes two commonly used mobile phone home location query programs. One is to use APIs to call third-party interfaces, and the other is to use our own mobile phone database to query and then judge the mobile phone number home location query.

First, let's look at a program that calls the third method website.

It is mainly implemented using curl and requires php support for curl.

If you are using a windows system in your php. INI File Settings, find php_curl.dll and cancel the semicolon comment, as shown below:
// Cancel the comment

Extension = php_curl.dll

If you are in Linux, You need to recompile your PHP. During editing, you need to open the compilation parameters and add the "-with-curl" parameter to the configure command.

Restart apache

The Code is as follows: Copy code

<? Php
Header ("Content-Type: text/html; charset = UTF-8 ");
If (isset ($ _ GET ['number']) {
$ Url = 'HTTP: // webservice.webxml.com.cn/webservices/mobilecodews.asmx/getmobilecodeinfo ';
$ Number = $ _ GET ['number'];
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, "mobileCode = {$ number} & userId = ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ Data = curl_exec ($ ch );
Curl_close ($ ch );
$ Data = simplexml_load_string ($ data );
If (strpos ($ data, 'HTTP ://')){
Echo 'mobile Phone Number Format error! ';
} Else {
Echo $ data;
}
}
?>
<Form action = "mobile. php" method = "get">
Mobile phone number: <input type = "text" name = "number"/> <input type = "submit" value = "submit"/>
</Form>

Instance 2
This instance uses the mobile phone database to store files locally and then find the database. However, you must update the database frequently.

The Code is as follows: Copy code

<Html>
<Head>
<Title> mobile phone number attribution query </title>
</Head>
<Style type = "text/css">
# Main {
Height: 100%;
}
# Left {
Float: left;
Height: 100%;
Width: 10%;
Float: left;
}
# Right {
Float: left;
Height: 100%;
Width: 100 $;
}
</Style>
<Body>
<? Php
Require ('function. php ');
$ Pwd = "xiaolin"; // password. Change it.
If (! Isset ($ _ GET ["action"]) {
Header ("Location: manage. php? Action = login ");

}
If ($ _ GET ["action"] = "login "){

?>
<Form action = "manage. php? Action = loginin "method =" POST ">
<P> enter the password: </p>
<P> <input type = "password" value = "" name = "pwd"> </p>
<P> <input type = "submit" value = "Logon"> </p>
</Form>
<? Php
} Elseif ($ _ GET ["action"] = "loginin "){
$ Repwd = $ _ POST ["pwd"];
If ($ repwd! = $ Pwd ){
Echo "Sorry, incorrect password ~! ";
} Else {
$ _ SESSION ["flag"] = true;
Leftnav ();
}
 
} Elseif ($ _ GET ["action"] = "info "){
Leftnav ();
Getinfo ();
?>
</Div>
</Div>
<? Php
} Elseif ($ _ GET ["action"] = "edit "){
Leftnav ();
?>
<Form action = "manage. php? Action = editp "method =" POST ">
<P> mobile phone number: <input type = "text" name = "num"> (at least 7 digits) </p>
<P> details: <input type = "text" name = "info"> </p>
<Input type = "submit" value = "Update">
</Form>
 
<? Php
 
} Elseif ($ _ GET ["action"] = "loginout "){
If (isset ($ _ SESSION ["flag"]) {
Unset ($ _ SESSION ["flag"]);
Session_destroy ();
}
Echo "<p> <a> logout </a> </p> <a href = 'Manage. php '> return </a> </p> ";


} Elseif ($ _ GET ["action"] = "editp "){
Leftnav ();
$ Num = $ _ POST ["num"];
$ Info = $ _ POST ['info'];
If (! Empty ($ num )&&! Empty ($ info) & strlen ($ num) >=7 ){
Update ($ num, $ info );
} Else {
Die ("cannot be blank! ");
}
} Elseif ("about" ==$ _ GET ["action"]) {
Leftnav ();
Showabout ();
}
?>
</Body>
</Html>

Index. php

<Html>
<Head>
<Title> mobile phone number attribution query </title>
</Head>
<Body>
<Form action = "index. php? Action = search "method =" POST ">
<P> enter the phone number you want to query: <input type = "text" name = "phone"> </p>
<P> <input type = "submit" value = ""> </p>
</Form>
<? Php
If (isset ($ _ GET ["action"]) {
If ("search" ==$ _ GET ["action"]) {
Require ('function. php ');
$ Phone = (isset ($ _ POST ["phone"])? $ _ POST ["phone"]: die ("Please return ");
Echo "your phone number <font color = red> ". $ phone. "</font> belongs to <font color = red> ". getphone ($ phone ). "</font> ";
}
}
?>
</Body>
</Html>

Function. php Functions

<? Php

Session_start ();

Function update ($ num, $ info ){
$ Dbpath = "xiaolin /";
$ Len = strlen ($ num );
If ($ len <7 ){
Return "The minimum mobile phone number is 7 characters ";
}
$ Par = "[0-9]";
For ($ I = 0; $ I <$ len; $ I ++ ){
If (! Ereg ($ par, substr ($ num, $ I, 1 ))){
Return "the mobile phone number can only be a number ";
}
}
$ Sunum = scandir ($ dbpath); // get the supported mobile phone number prefix
Array_splice ($ sunum,); // cancel the current directory
Array_splice ($ sunum,); // remove the upper-level directory
$ Sub = substr ($ num,); // obtain the first three digits of the number.
If (in_array ($ sub, $ sunum )){
$ Num1 = ltrim (substr ($ num, 3, 4), "0 ");
$ Search = file ($ dbpath. $ sub );
$ Tmp = $ search [$ num1];
$ Search [$ num1] = $ num1. '='. $ info. "n ";
$ Fp1 = fopen ($ dbpath. $ sub. '1', 'wb + ');
For ($ I = 0; I I <10000; $ I ++ ){
// $ Phone = str_pad ($ I, 4, "0", STR_PAD_LEFT );
// $ Phoneinfo = "";
// $ Phoneinfo. = $ phone. "= ";
// $ Phoneinfo = (isset ($ search [$ I])? $ Search [$ I]: "";
// $ Phoneinfo. = "n ";
Fwrite ($ fp1, $ search [$ I]);
}
Fclose ($ fp1 );
Echo "$ num updated ";
} Else {
Die ("$ sub is not supported currently ");
}
}
Function getphone ($ phone ){
$ Dbpath = "xiaolin /";
$ Len = strlen ($ phone );
If ($ len <7 ){
Return "The minimum mobile phone number is 7 characters ";
}
$ Par = "[0-9]";
For ($ I = 0; $ I <$ len; $ I ++ ){
If (! Ereg ($ par, substr ($ phone, $ I, 1 ))){
Return "the mobile phone number can only be a number ";
}
}
$ Sunum = scandir ($ dbpath); // get the supported mobile phone number prefix
Array_splice ($ sunum,); // cancel the current directory
Array_splice ($ sunum,); // remove the upper-level directory
$ Sub = substr ($ phone,); // obtain the first three digits of the number.
If (in_array ($ sub, $ sunum )){
$ Num = ltrim (substr ($ phone, 3,4), "0 ");
$ Search = file ($ dbpath. $ sub );
$ Tmp = $ search [$ num];
$ Result = substr ($ tmp, strpos ($ tmp, "=") + 1, strlen ($ tmp)-strpos ($ tmp, "=")-2 ); // process data
Return (strlen ($ result)> 1 )? $ Result: "No data ";
} Else {
Return "$ sub" is not supported currently ";
}
}

Function check (){
If (! Isset ($ _ SESSION ["flag"]) {
Die ("<p> Please <a href = 'Manage. php? Action = login '> log on! </A> </p> ");
} Elseif ($ _ SESSION ["flag"]! = True ){
Die ("<p> Please <a href = 'Manage. php? Action = login '> log on! </A> </p> ");
}
}

Function getinfo (){
Check ();
$ Nums = array ("130", "131", "132", "133", "134", "135", "136", "137 ", "138", "139", "150", "151", "153", "155", "156", "157", "158 ", "159 ");
$ Counts = "";
For ($ j = 0; $ j <count ($ nums); $ j ++ ){
$ Id = $ j;
If ($ id> = count ($ nums) {die ("OVER ");}
$ Nownum = $ nums [$ id]; // The current number segment
$ Dbpath = "xiaolin /";
$ Fp = fopen ("xiaolin/$ nownum", 'R ');
While (! Feof ($ fp )){
$ Line = fgets ($ fp );
$ Tmp = explode ("=", $ line );
$ Num1 [$ tmp [0] = substr ($ line, strpos ($ line, "=") + 1, strlen ($ line)-strpos ($ line, "=")-2 );
}
Fclose ($ fp );
$ Flag = 0;
For ($ I = 0; I I <10000; $ I ++ ){
$ Ser = str_pad ($ I, 4, "0", STR_PAD_LEFT );
If (! Strlen ($ num1 [$ ser]) = 0 ){
++ $ Flag;
}
}
$ Counts + = $ flag;
Echo "$ nownum: Segment record $ flag </p> ";
}
Echo "total $ counts ";}
Function leftnav (){
Check ();
?>
<Div>

<Div id = "right">

<? Php
}
Function showabout (){
Echo "<p> mobile phone number attribution query </p>
";
}
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.