Aspect:
1. file_get_contents timeout control.
2. Page encoding judgment.
3. Press Enter to capture the response.
4. handle keyboard event compatibility. // Event = event | window. event;
5. XMLHttpRequest and jQuery implementation solutions.
6. The page and source code are displayed at the same time.
XMLHttpRequest version get_web.php
Copy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = UTF-8 ");
If (! Empty ($ _ POST ['input _ text']) {
Ini_set ('default _ socket_timeout ', 10 );
If (! $ Data = file_get_contents ($ _ POST ['input _ text']) {
Echo "Time out! ";
Return;
}
$ Charset_pos = stripos ($ data, 'charset ');
If ($ charset_pos ){
If (stripos ($ data, 'utf-8', $ charset_pos )){
Echo iconv ('utf-8', 'utf-8', $ data );
} Else if (stripos ($ data, 'gb2312', $ charset_pos )){
Echo iconv ('gb2312', 'utf-8', $ data );
} Else if (stripos ($ data, 'gbk', $ charset_pos )){
Echo iconv ('gbk', 'utf-8', $ data );
}
Return;
}
Echo $ data;
} Else {
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Get Web Page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta http-equiv = "Content-Language" content = "zh-CN"/>
<Script type = "text/javascript">
Function createXMLHTTP ()
{
Try
{
Var request = new XMLHttpRequest ();
}
Catch (e1)
{
Var arrVersions = ["Microsoft. XMLHTTP", "MSXML2.XMLHttp. 4.0 ",
"MSXML2.XMLHttp. 3.0", "MSXML2.XMLHttp. 5.0"];
For (var I = 0; I <arrVersions. length; I ++ ){
Try {
Request = new ActiveXObject (arrVersions [I]);
} Catch (e2 ){
Request = false;
}
}
}
Return request;
}
Function ajax_post (url, params, target_id)
{
Request = new createXMLHTTP ();
Request. onreadystatechange = function (){
If (this. readyState = 4)
If (this. status = 200)
If (this. responseText! = Null)
Document. getElementById (target_id). innerHTML = this. responseText;
}
Request. open ("POST", url, true );
Request. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
Request. setRequestHeader ("Content-length", params. length );
Request. setRequestHeader ("Connection", "close ");
Request. send (params );
}
Var checked = false;
Function check _ (value ){
Checked = value;
}
Function get_key (event ){
Event = event | window. event;
If (event. keyCode = 13 & checked! = False)
{
Var url = document. getElementById ('input _ text'). value;
If (url! = ''){
Get_page ();
} Else {
Document. getElementById ('input _ text'). onfocus ();
Return false;
}
}
}
Function get_page (){
Var url = document. getElementById ('input _ text'). value;
If (! Url ){
Return false;
} Else {
If (document. getElementById ('output _ page'). innerHTML! = ''){
Document. getElementById ('output _ page'). innerHTML = '';
}
}
If (url. indexOf ('HTTP: // ') =-1 ){
Url = 'HTTP: // '+ url;
}
Ajax_post (
'<? Php echo $ _ SERVER ['php _ SELF '];?> ',
'Input _ text = '+ url,
'Output _ page'
);
Document. getElementById ('click _ show'). style. display = 'block ';
Document. getElementById ('back _ a'). href = document. location. href;
Document. getElementById ('origin _ website'). href = url;
}
</Script>
<Style>
. Div_box {
Margin-top: 10px;
}
. Input_box {
Border: 1px solid;
Margin-left: 10px;
Margin-top: 2px;
Height: 15px;
Float: left;
Size: 32
Font-size: 14px;
}
. Button_box {
Float: left;
Height: 23px;
Padding-bottom: 3px;
}
. Hide_box {
Display: none;
}
. A_box {
Margin-left: 10px;
Margin-top: 3px;
Height: 15px;
Float: left;
Font-size: 14px;
}
. Clear_box {
Height: 50px;
}
</Style>
</Head>
<Body onkeydown = "get_key (event)">
<Div class = "div_box">
<Input id = "input_text" class = "input_box" type = "text" value = "" onclick = "check _ (true)" onblur = "check _ (false) "> </input>
<Input type = "button" class = "button_box" onclick = "get_page ()" value = "Get it! "> </Input>
<Div id = "click_show" class = "hide_box">
<A id = "origin_website" class = "a_box" href = "#" target = "_ black"> access the origin site </a>
<A id = "back_a" class = "a_box" href = "#"> back </a>
</Div>
</Div>
<Div class = "clear_box"> </div>
<Div id = "output_page"> </div>
</Body>
</Html>
<? Php
}
// End_php
JQuery version get_web.phpCopy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = UTF-8 ");
If (! Empty ($ _ POST ['input _ text']) {
Ini_set ('default _ socket_timeout ', 10 );
If (! $ Data = file_get_contents ($ _ POST ['input _ text']) {
Echo "Time out! ";
Return;
}
$ Charset_pos = stripos ($ data, 'charset ');
If ($ charset_pos ){
If (stripos ($ data, 'utf-8', $ charset_pos )){
Echo iconv ('utf-8', 'utf-8', $ data );
} Else if (stripos ($ data, 'gb2312', $ charset_pos )){
Echo iconv ('gb2312', 'utf-8', $ data );
} Else if (stripos ($ data, 'gbk', $ charset_pos )){
Echo iconv ('gbk', 'utf-8', $ data );
}
Return;
}
Echo $ data;
} Else {
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Get Web Page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta http-equiv = "Content-Language" content = "zh-CN"/>
<Script type = "text/javascript" src = "http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ (Document). keyup (function (e ){
E = e | window. event;
If (e. keyCode = 13 & $ ("# input_text"). val ()! = ''){
$ (". Button_box"). click ();
}
});
$ (". Button_box"). click (function (){
If ($ ("# input_text"). val () = ''){
$ ("# Input_text"). addClass ('errortids'). focus ();
Return false;
} Else {
$ ("# Input_text"). removeClass ('errortids ');
}
$. Ajax ({
Url: '<? Php echo $ _ SERVER ['php _ SELF ']?> ',
Data: 'input _ text = '+ $ ("# input_text"). val (),
Type: 'post ',
Success: function (msg ){
$ (". Html_tips"). show ();
$ ("# Origin_website"). attr ('href ', $ ("# input_text"). val ());
$ ("# Back_a"). attr ('href ', document. location. href );
$ ("# Click_show"). show ();
$ ("# Output_page_html" ).empty().val(msg).css ({height: parseInt ($ (document). height ()-100)}). show ();
$ ("# Output_page" ).empty().html (msg). show ();
}
});
});
});
</Script>
<Style>
. Div_box {
Margin-top: 10px;
}
. Input_box {
Border: 1px solid;
Margin-left: 10px;
Margin-top: 2px;
Height: 15px;
Float: left;
Size: 32
Font-size: 14px;
}
. Button_box {
Float: left;
Height: 23px;
Padding-bottom: 3px;
}
. Hide_box {
Display: none;
}
. A_box {
Margin-left: 10px;
Margin-top: 3px;
Height: 15px;
Float: left;
Font-size: 14px;
}
. Clear_box {
Height: 50px;
}
. Error_tips {
Border: 1px solid red;
}
# Output_page_html {
Width: 960px;
Margin: 0 auto;
}
. Html_tips {
Float: left;
Margin: 0 21px;
Font-size: 1.8em;
}
</Style>
</Head>
<Body>
<Div class = "div_box">
<Input id = "input_text" class = "input_box" type = "text" value = ""> </input>
<Input type = "button" class = "button_box" value = "Get it! "> </Input>
<Div id = "click_show" class = "hide_box">
<A id = "origin_website" class = "a_box" href = "#" target = "_ black"> access the origin site </a>
<A id = "back_a" class = "a_box" href = "#"> back </a>
</Div>
</Div>
<Div class = "clear_box"> </div>
<Div class = "html_tips hide_box"> site </div>
<Div id = "output_page"> </div>
<Div class = "html_tips hide_box"> site source code </div>
<Textarea id = "output_page_html" class = "hide_box"> </textarea>
</Body>
</Html>
<? Php
}
// End_php
Author: Zjmainstay