doesnt display anything on the browser when using filename.php - php

it produces output when it is saved as filename.html
but when i try to change it to filename.php
then open the link in the brown using filename.php
it doesnt produce output anymore
NOTE: i removed some of the code too long
<?php
echo"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
print code requirement
function printpage()
{
window.print();
}
</script>
print code requirement
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>WildFlowers by FCT</title>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css'>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
#wrapper #header-wrapper #header h1 a strong {
font-family: Georgia, "Times New Roman", Times, serif;
size: '100';
font-size: 40px;
</body>
</html>
";
?>

You have to remove <?php and ?>. Between these two tags there must be php-code and no html.
Edit: Maybe no errors are reported, because your server is configured to suppress errors.

Put some space after the echo..And use single quote['] insted of double.And change all single quotes from your code to double quote.(")
eg: -
<?php echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body> Bla bla bla
</body>
</html>';
?>
The problem in your code is, you just started the codes with double quote.So the PHP server consider the next double quote is the closing of the echo statement.So there is nothing to print inside the first two double quote.But if you take the page source, you can see
<!DOCTYPE html PUBLIC

This is because you open PHP tag at the beginning of file:
<?php
Close it before your HTML markup:
<?php
// your php code
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

You can not have html in your php tags, if you want to use html you have to end the close your php first so something like this:
<?php
// php code here
?>
html code here
<?php
// php here
?>

If You need to write html inside php code use the echo statement.
ex:
<?php echo"
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head></head>
<body>
//your code here
</body>
</html>";
?>

Related

$_GET in PHP not getting utm values

I am trying to print / echo utm_source, utm_medium and utm_campaign query values using this PHP code;
echo $_GET['utm_source'];
But strangely for some unknown reason on my server its not printing the values, when I replace utm_source to something else like test_source I am able to see the value in print.
Never faced any such issue, can anyone guide me here.
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $_GET['utm_source']; ?>
</body>
</html>
If your site is using WPengine, you will not be able to get the utm variables (and any additional variables after the utm's) out of the URL using PHP. Some web hosts specifically target and remove these utm parameters from server requests for performance reasons. Tricky part is, utm variables are still going remain in the URL which means that you can retrieve them using Javascript instead if you need to.
Source: WPengine article
https://wpengine.com/support/utm-gclid-variables-caching/
Your original question quoted this code
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php $_GET['utm_source']; ?>
</body>
</html>
If you add an echo to this statement it will work
<?php echo $_GET['utm_source']; ?>
when tested using
test.com/test.php?utm_source=1
A safer piece of PHP code would be
<?php
if ( isset($_GET['utm_source']) ) {
echo $_GET['utm_source'];
} else {
echo 'the parameter utm_source is missing';
}
?>
I know it's late. but maybe this answer helps another developer.
// get the value of outlink, source, campaign, medium, content
let url = new URL(window.location.href);
let outlink = url.searchParams.get('outlink'),
utmSource = url.searchParams.get('utm_source'),
utmCampaign = url.searchParams.get('utm_campaign'),
utmMedium = url.searchParams.get('utm_medium'),
utmContent = url.searchParams.get('utm_content');
console.table( { outlink , utmSource, utmCampaign,
utmMedium,utmContent});

</script> in PHP heredoc

Why I am not able to end a javascript inside a PHP heredoc?
The rest of the code below this line:
</script>
become not part of PHP's code. They become HTML code.
It is like the end script code ends the PHP block.
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
</script> // Here is the problem
</head> // code here and below becomes not part of PHP.
<body>
.
.
.
HEAD;
Any tips for solving this problem?
Although I can't reproduce this with HEREDOC (it's possible that different versions of PHP behave differently in this respect), </script> is equivalent to ?> in PHP code, because it's a counterpart to <script language="php">. Example:
<script language="php"> $a = 1; </script>
Test: <?= $a ?>
So wherever you encounter problems with the ?> closing tag, you'll also encounter the same problems with the </script> closing tag. One option would be to store this in a variable and use it. Example:
<?php
$endScript = '</' . 'script>';
$headerContent = <<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>$title</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
<script>
$endScript
</head>
<body>
.
.
.
HEAD;

three empty lines in the begin of html page

i have html code inside a php variable
<?
$HTML_CODE='
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>'.$pg->pgTitle.'</title>
<link rel="stylesheet" href="'.$cssFile.'" type="text/css" />
</head>
<div id="header">
';
echo $HTML_CODE;
?>
When i view the source of my page by in browser ( right click-> view source )
The code starts in line 4
1.
2.
3.
4.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
5.<html>
6.<head>
7.<title>page title</title>
8.<link rel="stylesheet" href="style.css" type="text/css" />
9.</head>
10.<div id="header">
i've used trim function, it's now start in line 3!!
This isn't an issue; no errors will result, but at least one of those lines is due to your code:
$HTML_CODE='
<!DOCTYPE HTML PUBLIC ...
this introduces a line feed in. Additionally, I expect you have some whitespace before your php opening tag <?.
try
<? $HTML_CODE='<!DOCTYPE
with out the line breaks in your code, also try removing the linebreak between
'; and echo
Remove the unnecessary page breaks from your initialisation of variable
$HTML_CODE='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>'.$pg->pgTitle.'</title>
<link rel="stylesheet" href="'.$cssFile.'" type="text/css" />
</head>
<div id="header">';

PHP string with accentuated characters not displayed properly

I am on PHP 5.2.17 (and I am no PHP expert). I was hoping the following would display properly:
<?php
$title = "Jérôme";
echo $title."<br>";
?>
But it displays:
Jérôme
How can display my string properly? (The string is static)
Add to your HTML head:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
Note that you should have a proper HTML doctype because browsers default to non utf8. You can do a simple test, like I did, this works:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
$title = "Jérôme";
echo $title."<br>";
But the place for the meta tag is in the head tag. The HTML document should look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>An XHTML 1.0 Strict standard template</title>
<meta http-equiv="content-type"
content="text/html;charset=utf-8" />
</head>
<body>
<?php
$title = "Jérôme";
echo $title."<br>";
?>
</body>
</html>
That is standard.
<meta http-equiv="content-type" charset="utf-8" content="text/html;" />
<?php
$title = "Jérôme";
echo htmlspecialchars($title);
?>
Use php htmlentities function. see below example
$title = "Jérôme";
$title= htmlentities($title);
echo "<BR>Title :".$title."<br>";

php won't work for some reason

Well, I'm trying to get an iframe from a URL of this sort: http://mydomain.com/frame.php?q=http://someotherdomain.com
For some reason I am getting a server error with this code and cannot figure out why. Does anyone see something wrong?
Thanks!
<?php
$q = $_GET('q');
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
};
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo getTitle($q) ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php echo "<iframe src='".$q."' style='width:100%;height:100%;border:0;'></iframe>" ?>
</body>
</html>
Well, for starters, this
$q = $_GET('q');
should be
$q = $_GET['q'];
PLEASE NOTE
This method you are using is highly insecure. Consider a malicious person making the following request.
http://mydomain.com/frame.php?q=..%2F..%2F..%2F..%2Fetc%2Fapache2%2Fhttpd.conf
By providing different values of q, the attacker can potentially read any file readable by the webserver's user.
Try this:
<?php
$URL = parse_url($_SERVER['REQUEST_URI'];
/* make sure the title is the first parameter and only param passed, otherwise include a ltrim to get rid of everything starting with & */
$q = rtrim('=', $URL['query']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php=$q?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="margin: 0;">
<?php='<iframe src="' . $whateverURL . '" style="width: 100%; height: 100%; border: 0px;" />'?>
</body>
</html>
take a look at your apache log error
it is usually at:
tail -f /var/log/apache2/error.log

Categories