PHP, fopen, Browser Compatibility – What can it be - php

On this page http://www.effectivewebsolutions.biz/video-spokesmodel.html if you put in your URL it opens it through fopen function and puts a video spokes-model on the website for demonstration purposes, here is the script.
<?php
$handle = fopen($_POST["url"], "r");
while($data = fread($handle, 1000000)){
$contents .= $data;
}
fclose($handle);
echo "<base href=\"{$_POST['url']}\">";
echo "\n\n";
echo "<!-- Begin inserted page -->";
echo "\n";
echo $contents;
echo "\n";
echo "<!-- End inserted page -->";
echo "\n\n";
echo '<script type="text/javascript" src="http://www.internet-spokesmodels.com/scripts/swfobject.js"></script>';
echo '<style type="text/css" media="screen">object { outline:none; } </style>';
echo "\n";
echo '<script type="text/javascript" src="http://www.internet-spokesmodels.com/actors/script/SabrinaEXAMPLESredshirt_350x500.js"></script>';
?>
However in Safari it only opens text version of the website (no css or images).
It doesn’t make sense why would browser make a difference in this case.
Any Ideas?

Probably because the page does not render valid HTML. When I tried it with http://www.google.ca, I got:
<base href="http://google.ca">
<!-- Begin inserted page -->
<!doctype html><html><head><meta http-equiv="content-type" ...
The DOCTYPE should be the first thing in the page, followed by a <html> tag with HTML content. The <base> tag should be within the <head> tag.
You can't blame Safari for displaying invalid HTML incorrectly.

The HTML code being returned should be identical across browsers, since browsers have nothing to do with server-side code. Check the validity of HTML. Also, sanitize your input as suggested meagar.

Related

Simple html dom can find PHP script (<?php ... ?>)?

I am using simple-html-dom for my work. I want to get all PHP script (<?php ... ?>) form file using simple-html-dom.
if i have one file (name: text.php) with below code :
<html>
<head>
<title>Title</title>
</head>
<body>
<?php echo "This is test Text"; ?>
</body>
</html>
then how can i get this PHP script <?php echo "This is test Text"; ?> form above file of code using simple-html-dom.
$html = file_get_html('text.php');
foreach($html->find('<?php') as $element) {
//Sonthing code ...
}
i can not use like this, Is there any other option for this ?
Here's a solution using regex. Note that regex often is not advisable for parsing HTML files. That is, it might be okay in this case.
This will match each instance of a PHP code block and allow you to output (or do whatever else you want) either the entire block (including the tags) or the code that is contained within the block. See the documentation for preg_match_all().
<?php
$string = <<<'NOW'
<html>
<head>
<title>Title</title>
<?php echo "something else"; ?>
</head>
<body>
<?php echo "This is test Text"; ?>
</body>
</html>
NOW;
preg_match_all("/\<\?php (.*) \?\>/", $string, $matches);
foreach($matches[0] as $index => $phpBlock)
{
echo "Full block: " . $phpBlock;
echo "\n\n";
echo "Command: " . $matches[1][$index];
echo "\n\n";
}
DEMO

Import stylesheet with php if a certain page is requested

I would like to import a css stylesheet in a page depending on a php condition (or other), this condition is based upon the domain URL.
For example, if the page loaded is "mydomain.com/about-us" import a "css/about-us.css" file.
I have tried with this code, but it does not work.
<?php
$url = $_SERVER['REQUEST_URI'];
if (strstr($url, "mydomain.com/about-us/")) {
include '/css/about-us.css';
}
?>
How can I import, or use a <style> tag conditionally?
solution correct:
the correct solution is use only the page name, so if you page is mydomain.com/about-us/
use " /about-us/" only.
now have other question, with the code posted you can import css for specific page , but I noticed that if the domain is mydomain.com/about-us/team.html example in the page team.html load also the css of "about-us" how load the css for about-us only in the page mydomain/about-us/ ??
How you can read here, strstr will return a string or FALSE. You can change it like this:
<!DOCTYPE html>
<head>
<?php
if (strstr($_SERVER['REQUEST_URI'], "mydomain.com/about-us/")!=false) {
echo '<link rel="stylesheet" type="text/css" href="/css/about-us.css">';
} ?>
</head>
...
</body>
</html>
Or:
<!DOCTYPE html>
<head>
<style type="text/css">
<?php
if (strstr($_SERVER['REQUEST_URI'], "mydomain.com/about-us/")!=false) {
echo file_get_contents('/css/about-us.css');
} ?>
</style>
</head>
...
</body>
</html>
In the first example your CSS is included through the <link> tag, in the second, the PHP-script loads your CSS file into the script-tags. You can not use include because it will load another php file and execute where it was included. You should use my first example, because it is more server-friendly because the CSS file doesn't need to be read. Your page will be faster.
You can add a stylesheet to the page with PHP by including this in the <head> of your html document:
<?php
echo '<link rel="stylesheet" type="text/css" href="' . $file . '">';
?>
Where $file is the name of the css file. You're going to have to provide some more information as to what you're trying to do for a better answer.
Update
The variable $_SERVER[REQUEST_URI] only gives the requested page, not the whole domain. From the PHP manual,
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
So the code should look as follows:
<?php
$requested_page = $_SERVER['REQUEST_URI'];
if ($requested_page === "/about-us" || $requested_page === "/about-us/") {
echo '<link rel="stylesheet" type="text/css" href="/css/about-us.css">';
}
?>
This will test if the requested page is "/about-us" (the client is requesting the "about-us" page) and if it does, the link to the stylesheet will be echoed.
use this:
<?php
$url = $_SERVER['REQUEST_URI'];
if (strstr($url, "mydomain.com/about-us/"))
{
// output an HTML css link in the page
echo '<link rel="stylesheet" type="text/css" href="/css/about-us.css" />';
}
else
{
// output an HTML css link in the page
echo '<link rel="stylesheet" type="text/css" href="/css/another.css" />';
}
?>
you can also do this to import the css contents directly, but probably some media/images links can break:
<?php
$url = $_SERVER['REQUEST_URI'];
if (strstr($url, "mydomain.com/about-us/"))
{
// output css directly in the page
echo '<style type="text/css">' .file_get_contents('./css/about-us.css').'</style>';
}
else
{
// output css directly in the page
echo '<style type="text/css">' .file_get_contents('./css/another.css').'</style>';
}
?>

how to echo php file without pars?

how to read a php file and echo it in a html file?
i try to use readfile() , file() file_get_content() to read a php file.
but when i echo file_string its parsed and then show.
how i can prevent to pars stirng var that included php codes.
here my code:
<?php
$path = '..../ex.php';
$source = fopen($path , "r");
echo fread($source,filesize($path ));
fclose($source);
?>
how to echo $source without compiled or parsed.
With this function
<?php
echo htmlspecialchars($text);
?>
php.net/htmlspecialchars
fread should works fine. Remember that when you use echo it prints <?php opening tag and in rendered page it can be not visible.
To test it, just try with var_dump:
$content = fread($source,filesize($path));
var_dump($content);
I'll go out on a limb and guess that the code is not showing up completely in your HTML page, because the browser is trying to interpret <?php as HTML tags. The solution is to HTML encode any text which may contain characters with a special meaning in HTML:
echo htmlspecialchars(file_get_contents('..../ex.php'));
See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).
use this
$path = 'ex.php';
$source = fopen($path , "r");
echo "<textarea style='border:0px; overflow: hidden; width:100%; height:100% '>";
echo fread($source,filesize($path ));
echo "</textarea>";
fclose($source);

Why are the html-tags inside <noscript> shown as text

I have a noscript part:
<noscript>
<h1><?php echo $php_title; ?></h1>
<div><?php echo $php_abstract; ?></div>
</noscript>
When I try this in a .html file (with the php tags removed, of course) it works as expected, but when in a .php file I get this visible output in the browser (i.e. the is not treated as an html tag):
<h1>Stephen Porges "The Polyvagal Theory"</h1>
I do not set any special headers with PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'HTTP/Request2.php';
$url = "https://api.zotero.org/groups/56508/items/3B6TR25A?format=atom&content=json";
$r = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$r->setConfig(array(
'ssl_verify_peer' => FALSE,
'ssl_verify_host' => FALSE
));
try {
$response = $r->send();
if ($response->getStatus() == 200) {
$body = $response->getBody();
$xml = new SimpleXMLElement($body);
$php_content = $xml->content;
$php_json = json_decode($php_content);
$php_title = $php_json->title;
$php_abstract = $php_json->abstractNote;
}
} catch (HttpException $ex) {
echo $ex;
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
The document is returned as text/html according to the browser.
I use Chrome and there is maybe a bug here, but it seems strange that it works ok in .html but not .php if that is the reason.
Any idea of what is going on here?
This is an issue with chrome where it is not rendered the first time the browser loads the page with JavaScript disabled. If you refresh the page after it shows plain text, it should be rendered properly.
This issue has already been reported:
https://code.google.com/p/chromium/issues/detail?id=235158
One person from that website suggested a workaround of using something like:
<div id="noscript">What was in the noscript-tag ..... </div>
<script type="text/javascript">
document.getElementById('noscript').style.display="none";
// rest of script
</script>
Therefore, it would only hide the elements if JavaScript is enabled.
I wouldn't say that it is that big of a deal. For example, Stack Overflow uses <noscript> tags regardless and the same occurs.

Is output buffering possible inside of flush?

I'm trying to show function status as its looping but delete the previous status before the next one shows. Current code:
<?php
#ini_set('zlib.output_compression',0);
#ini_set('implicit_flush',1);
#ob_end_clean();
set_time_limit(0);
// Perform 1st function here
echo "Retrieving Data...";
echo str_repeat(' ',1024*64);
sleep(1);
// Perform 2nd function here
echo "Analyzing Data...";
echo str_repeat(' ',1024*64);
sleep(1);
// Perform 3rd function here
echo "Done...";
echo str_repeat(' ',1024*64);
sleep(1);
// Clean all echos here..
?>
<html>
<head>
// Dynamic head content as a result of the php functions above
</head>
<body>
</body>
</head>
Now this works, but displays all the echos one after the other. I'd like the next status to replace the first, until the end, then remove "Done" before the html is displayed.
I tried:
ob_start();
echo "Retrieving Data...";
echo str_repeat(' ',1024*64);
sleep(1);
ob_end_clean();
But that didn't work. Is this possible at all?
The following uses the last-of-type pseudo CSS selector to hide all the old progress status messages. It's a reasonably new selector so it doesn't work well on older browsers (pre IE9), you can check the compatibility on the Mozilla Developer Network
<html>
<head>
<style type="text/css">
#progress span {
display: none;
}
#progress span:last-of-type {
display: block; !important
}
</style>
</head>
<body>
<div id="progress">
<?php
for ($i =0; $i<=100; $i+=10) {
ob_start();
echo "<span>$i%</span>";
ob_end_clean();
sleep(1);
}
?>
</div>
</body>
</html>
Output your lines with a \r. This will return the cursor to 0 on the same line. Where you can write over it.
echo "Retrieving Data..\r";
sleep(3);
echo "Analyzing Data...\r";
sleep(3);
echo "Done... \r\n";
I added a \n to the last echo otherwise the command prompt would over write the last echo.

Categories