/    Sign up×
Community /Pin to ProfileBookmark

Why Php Code is Shown in Browser ?

Php Folks,

This is really weird!
Look at this code …
All tags intact but look at my attached img. Why php code is shown in Fire Fox ?

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-05-10/1589105161-224749-subins-web-crawler.png]

[code]

<!DOCTYPE html>
<html>
<head>
<title>Web Crawler in PHP</title>
</head>
<body>
<div id=”content” style=”margin-top:10px;height:100%;”>
<center><h1>Web Crawler in PHP</h1></center>
<form action=”index.php” method=”POST”>
URL : <input name=”url” size=”35″ placeholder=”http://www.subinsb.com”/>
<input type=”submit” name=”submit” value=”Start Crawling”/>
</form>
<br/>
<b>The URL’s you submit for crawling are recorded.</b><br/>See All Crawled URL’s <a href=”url-crawled.html”>here</a>.
<?php
include(“simple_html_dom.php”);
$crawled_urls=array();
$found_urls=array();
function rel2abs($rel, $base){
if (parse_url($rel, PHP_URL_SCHEME) != ”) return $rel;
if ($rel[0]==’#’ || $rel[0]==’?’) return $base.$rel;
extract(parse_url($base));
$path = preg_replace(‘#/[^/]*$#’, ”, $path);
if ($rel[0] == ‘/’) $path = ”;
$abs = “$host$path/$rel”;
$re = array(‘#(/.?/)#’, ‘#/(?!..)[^/]+/../#’);
for($n=1; $n>0;$abs=preg_replace($re,’/’, $abs,-1,$n)){}
$abs=str_replace(“../”,””,$abs);
return $scheme.’://’.$abs;
}
function perfect_url($u,$b){
$bp=parse_url($b);
if(($bp[‘path’]!=”/” && $bp[‘path’]!=””) || $bp[‘path’]==”){
if($bp[‘scheme’]==””){$scheme=”http”;}else{$scheme=$bp[‘scheme’];}
$b=$scheme.”://”.$bp[‘host’].”/”;
}
if(substr($u,0,2)==”//”){
$u=”http:”.$u;
}
if(substr($u,0,4)!=”http”){
$u=rel2abs($u,$b);
}
return $u;
}
function crawl_site($u){
global $crawled_urls;
$uen=urlencode($u);
if((array_key_exists($uen,$crawled_urls)==0 || $crawled_urls[$uen] < date(“YmdHis”,strtotime(‘-25 seconds’, time())))){
$html = file_get_html($u);
$crawled_urls[$uen]=date(“YmdHis”);
foreach($html->find(“a”) as $li){
$url=perfect_url($li->href,$u);
$enurl=urlencode($url);
if($url!=” && substr($url,0,4)!=”mail” && substr($url,0,4)!=”java” && array_key_exists($enurl,$found_urls)==0){
$found_urls[$enurl]=1;
echo “<li><a target=’_blank’ href='”.$url.”‘>”.$url.”</a></li>”;
}
}
}
}
if(isset($_POST[‘submit’])){
$url=$_POST[‘url’];
if($url==”){
echo “<h2>A valid URL please.</h2>”;
}else{
$f=fopen(“url-crawled.html”,”a+”);
fwrite($f,”<div><a href=’$url’>$url</a> – “.date(“Y-m-d H:i:s”).”</div>”);
fclose($f);
echo “<h2>Result – URL’s Found</h2><ul style=’word-wrap: break-word;width: 400px;line-height: 25px;’>”;
crawl_site($url);
echo “</ul>”;
}
}
?>
</div>
<style>
input{
border:none;
padding:8px;
}
</style>
</body>
</html>

[/code]

What is wrong in the code ? What is misfiring in the browser ?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerMay 10.2020 — Do you have any other pages using PHP, and do they do the same thing? First inclination is the PHP engine is off.
Copy linkTweet thisAlerts:
@tracknutMay 10.2020 — It appears to be happening in the included file simple_html_dom.php. Does that file close out php parsing?
Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2020 — @tracknut#1618288

Not sure. How am I supposed to find out ?
Copy linkTweet thisAlerts:
@SempervivumMay 11.2020 — I suspect there is no opening `&lt;?php</C> in <C>simple_html_dom.php`
Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2020 — @Nachfolger#1618284

Today, the php code is getting interpreted by the interpreter. Not sure what was wrong last night.

Anyway, I now get this new error:

**Fatal error: Cannot redeclare file_get_html() (previously declared in C:xampphtdocstestcrawler7simplehtmldom_1_9_1simple_html_dom.php:48) in C:xampphtdocstestcrawler7simple_html_dom.php on line 86**

The crawler I am running is here:

http://localhost/test/crawler/7/index.php

http://localhost/test/crawler/7/simplehtmldom_1_9_1/simple_html_dom.php

My include looks like this:
<i>
</i>include("simplehtmldom_1_9_1/simple_html_dom.php");


Line 86 in index.php looks like this:
<i>
</i>padding:8px;


Line 48 in simple_html_dom.php looks like this:
<i>
</i>function file_get_html(
Copy linkTweet thisAlerts:
@developer_webauthorMay 11.2020 — @Sempervivum#1618328

Actually, I just read your reply and checked. it has the opening but not the closing!

How come ? I downloaded it from the official site. That means, everyone downloading it is experiencing this same problem!

Nevertheless, same issue remains that I mentioned in my previous post!
Copy linkTweet thisAlerts:
@SempervivumMay 11.2020 — @developer_web#1618330
it has the opening but not the closing![/quote]As discussed here and stated in the manual the closing `?&gt;` is optional when at the end of the file:

https://stackoverflow.com/questions/19953483/php-end-tag

My editor removes it persistantly in this case and at first I didn't understand why.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...