How to properly display an HTML page after PHP script? - php

somehow I couldn't find an answer to the following problem when searching the web.
I'm not familiar with PHP and am trying to get the below PHP code, which I put right at the beginning of the file, to display the HTML page that follows, instead of "Welcome.".
Many thanks for your time!
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo "Welcome.";
?>
<!DOCTYPE html>
<html lang="De">
<head>
<meta charset="UTF-8"/>
...

I found the solution...
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo <<<HTML
--> HTML section/code <--
HTML;
?>

Related

Accessing properties in an include when setting that include in to a file

I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>

How use a php variable declared in the parent file in an included file

I'm trying to pass the php variable $shareURL = "someURL"; from the parent page of test.php into the included file of commentTest.php. Is this possible? If yes, please help.
Parent File = Test.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
$shareURL = "someURL";
echo "$shareURL";
include "http://domainName.net/assets/includes/commentTest.php";
?>
</body>
</html>
PHP Included File = commentTest.php
<?PHP
echo "<div class='fb-comments' data-href='$shareURL' data-num-posts='5' data-width='100%'></div>";
?>
HTML Output Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
someURL<div class='fb-comments' data-href='' data-num-posts='5' data-width='100%'></div></body>
</html>
Change your Test.php to this:
include "/assets/includes/commentTest.php";
Thanks everyone!
Your comments and answers helped me find the solution I needed.
$root = dirname(__FILE__);
include "$root/assets/includes/commentTest.php";
Apparently my root is here /var/www/html instead of right after the TLD in the URL.

How to convert php code to html source code?

I need to display html source code form other php file.
I have two file
code.php
index.php (I hope I can convert the code.php to html source code.)
code.php:
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
index.php (I hope I can convert the code.php to html source code.)
$php_to_html = file_get_contents("code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
but when i run the index.php file, the result is
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
but I hope I can see the result is
<!DOCTYPE html> <html> <body> red </body> </html>
any idea how can i do this ,thanks!!!
You want to execute the PHP, so include it and capture the output:
ob_start();
include("code.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
If you want the HTML to be rendered as HTML then don't use htmlentities().
Optionally (not the best way) but you can execute it by retrieving from the URL:
$php_to_html = file_get_contents("http://www.example.com/code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
Buffer output and include it:
ob_start();
include_once('code.php');
$html = ob_get_clean();
By using output buffering, any output is not sent to the browser, but instead kept in memory. This allows you to run the code, and get the output as a variable. ob_get_clean() flushes the buffer (in this case into our $html variable), and then stops buffering, allowing you to continue as normal. :-)

Returning Values php

All i know from the return statement (based on JS) that is stops execution of the script and return the value and whats after it ignored, then how is that possible
(in fact required) in this php code :
<?php
return "<!DOCTYPE html>
<html>
<head>
<title>$title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$content
</body>
</html>";
which is completing this :
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
$page = include_once "templates/page.php";
echo $page;
and also is it possible to have return outside a function ????
You were almost there...
With the template/page.php you don't need to run a return all you need to do is as follows:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
Then within the PHP page side of things lets say for example, phpCode.php:
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$title = "Test title";
$content = "<h1>Hello World</h1>";
include_once "templates.php";
Include will pull all of the information from the file you're including generally.
You can pull information from anywhere providing you have required the php file. PDO / OOP is a great example of how to do this and I'd highly suggest learning it somewhat more to achieve what you're trying to do (from what I could make out in the question).

Parse/Get HTML title element from a PHP file for an Ajax call

I am trying to get the title element's content that is contained in a echo statement of a PHP file.
I am using a PHP file for a website that when accessed by a Ajax call it returns only part of the page, but when accessed directly it returns the entire page.
That much is working fine. But I would like to change the title of the page when it is accessed via the Ajax call, the innerHTML of the title tag is what I'm trying to get.
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo '
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Products at Avrent</title>
<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />
With a HTML file this code works.
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
$html = file_get_html($url);
/* get page's title */
preg_match("/<title>(.+)<\/title>/siU", $html, $matches);
$title = $matches[1];
echo $title;
}
?>
But it returns gibberish when I try using it with a PHP file.
Can someone help me find a PHP script that will work on a PHP file?
Here's what I've gathered: you have a bunch of HTML pages. You have an index.php script that takes a URL, loads up the HTML from that URL, swaps out the title, then spits the HTML back out?
First of all, why do you have things set up like that? If you insist...
You (at the very least) should do this:
index.php
Remove the RegEx. You're using an HTML parser; use that!
<?php
if(isset($_GET['url'])) {
$url = $_GET['url'];
$html = file_get_html($url);
/* get page's title */
$title = $html->find('title', 0)->innertext;
echo $title;
}
?>
ajax_page.php
Set title from variable.
if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo '
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>' . $page_title . '</title>
<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />
Then, from index.php:
$page_title = "INSERT THE PAGE TITLE HERE";
require "ajax_page.php";

Categories