For example if I had the script:
<?php
$page = "My Page";
echo "<title>" . $page . "</title>";
require_once('header.php');
require_once('content.php');
require_once('footer.php');
?>
Is there something I can add to the bottom of that page to show the entire pre-compiled php?
I want to literally echo the php code, and not compile it.
So in my browser I would see the following in code form...
// stuff from main php
$page = "My Page";
echo "<title>" . $page . "</title>";
// stuff from require_once('header.php');
$hello = "Welcome to my site!";
$name = "Bob";
echo "<div>" . $hello . " " . $name . "</div>";
// stuff from require_once('content.php');
echo "<div>Some kool content!!!!!</div>";
// stuff from require_once('footer.php');
$footerbox = "<div>Footer</div>";
echo $footerbox;
Is this possible?
There's no way to do it native to PHP, but you could try to hack it if you just wanted something extremely simplistic and non-robust:
<?php
$php = file_get_contents($_GET['file']);
$php = preg_replace_callback('#^\s*(?:require|include)(?:_once)?\((["\'])(?P<file>[^\\1]+)\\1\);\s*$#m', function($matches) {
$contents = file_get_contents($matches['file']);
return preg_replace('#<\?php(.+?)(?:\?>)?#s', '\\1', $contents);
}, $php);
echo '<pre>', htmlentities($php), '</pre>';
Notes:
Warning: Allowing arbitrary file parsing like I've done with the fist line is a security hole. Do your own authentication, path restricting, etc.
This is not recursive (though it wouldn't take much more work to make it so), so it won't handle included files within other included files and so on.
The regex matching is not robust, and very simplistic.
The included files are assumed to be statically named, within strings. Things like include($foo); or include(__DIR__ . '/foo.php'); will not work.
Disclaimer: Essentially, to do this right, you need to actually parse the PHP code. I only offer the above because it was an interesting problem and I was bored.
echo '$page = "My Page";';
echo 'echo "<title>" . $page . "</title>";';
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
For clarity I'd put the title generation in it's own file, then just use a series of echo file_get_contents()...
echo file_get_contents('title.php');
echo file_get_contents('header.php');
echo file_get_contents('content.php');
echo file_get_contents('footer.php');
Related
I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.
I want to display all the session , post , get information of my php page (.php) in the page. How can I do that?
Ok, everybody states the obvious, but they do not mention the security risk embedded in every solution.
I beg to differ, here is my alternative:
echo "<pre>";
echo htmlspecialchars(var_export($_POST, true), ENT_QUOTES);
echo htmlspecialchars(var_export($_GET, true), ENT_QUOTES);
echo htmlspecialchars(var_export($_SESSION, true), ENT_QUOTES);
echo "</pre>";
You probably do not think about an attacker putting malicious code into your session, but you never know.
To put it different:
header("Content-type: text/plain");
var_export($_POST);
var_export($_GET);
var_export($_SESSION);
will equally work well, but beware browsers trying to sniff the content.
Another method that prints two thirds of the stuff nicely:
phpinfo(INFO_VARIABLES);
And at last: Try the <plaintext> tag. It has no closing tag for obvious reasons, because it will end the interpretation of HTML in the browser, but after it was sent, you'll only have to care about printing plain text.
echo "<plaintext>";
var_export($_POST);
var_export($_GET);
var_export($_SESSION);
var_export has an advantage above var_dump and print_r: It prints valid PHP code that is able to recreate the variable. This is true 100% for any type but objects. Objects have to implement the magic static method __set_state to be able to be recreated.
Use the below code to print / var_dump the information
For Session Is Below
<?php var_dump($_SESSION); ?>
For Post Is Below
<?php var_dump($_POST); ?>
For Get Is Below
<?php var_dump($_GET); ?>
<?php print_r($_SESSION); ?>
<?php print_r($_POST); ?>
<?php print_r($_GET); ?>
Try this :
POST
foreach ($_POST as $key => $value) {
$postData = "POST Field " . htmlspecialchars($key) . " is " . htmlspecialchars($value) . "<br>";
}
GET
foreach ($_GET as $key => $value) {
$postData = "GET Field " . htmlspecialchars($key) . " is " . htmlspecialchars($value) . "<br>";
}
SESSION
foreach ($_SESSION as $key => $value) {
$postData = "SESSION Field " . htmlspecialchars($key) . " is " . htmlspecialchars($value) . "<br>";
}
I prefer var_dump() over print_r(), as it handles objects as well.
I also like to output <pre> tags around my dump, so its easier to read in a browser, so
echo "<pre>";
var_dump($_SESSION);
var_dump($_POST);
var_dump($_GET);
echo "</pre>";
I want to know how to get the original URL from php
For example:
example.php
<?php
header('location:test.php');
?>
I want to get test.php from example.php.
example.php
<?php
header('location:' . $_SERVER['HTTP_REFERER']);
?>
This should work fine.
Redirection with some delay say after 5 sec wait.
function js_redirect($url, $seconds)
{
echo "<script language=\"JavaScript\">\n";
echo "<!-- hide code from displaying on browsers with JS turned off\n\n";
echo "function redirect() {\n";
echo "window.parent.location = \"" . $url . "\";\n";
echo "}\n\n";
echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";
echo "-->\n";
echo "</script>\n";
return true;
}
js_redirect("http://www.exapmle.com",5); // Redirect after 5 sec
you forget to use
ob_start();
in first of your code :D
elsewhere you can use js in this case , for example :
echo 'javascript:window.location="http://example.com";';
I have a function that is controlling the output of my page:
$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class='media-desc'>{$desc}</div>";
I would like to include a file "box.php" inside that html that is defined in the $page variable. I tried this:
$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . include("box.php"); . "</div><div class='media-desc'>{$desc}</div>";
... but it didn't work. How can I put a php include inside of a variable?
from php.net
// put this somewhere in your main file, outside the
// current function that contains $page
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
// put this inside your current function
$string = get_include_contents('box.php');
$page = '<div class="media-title"><h2>{$title}</h2></div>';
$page .= '<div class="media-image">{$image}</div>';
$page .= '<div class="inlinebox">' . $string . '</div>';
$page .= '<div class="media-desc">{$desc}</div>';
How can I put a php include inside of a variable?
# hello.php
<?php
return "Hello, World!";
?>
# file.php
$var = include('hello.php');
echo $var;
I would generally avoid such a thing though.
First, don't use a semicolon from inside the statement.
Second, wrap the include statement in parentheses.
$page = "<div class='media-title'><h2>{$title}</h2></div>
<div class='media-image'>{$image}</div><div class="inlinebox">" .
(include "box.php") . "</div><div class='media-desc'>{$desc}</div>";
Finally: In the "box.php" file, you will need to do the following:
<?php
ob_start();
// your code goes here
return ob_get_clean();
EDIT: Some info about calling return outside of the function contest: PHP Manual - Return.
Edit:
Don't know if this is useful, but i think that including a file to get a piece of HTML, is not a good option. It's not scalable. You could try with something like MVC. You could ask your controller to renderize the content of what you want.
$view = $controler->getElement('box');
$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . $view . "</div><div class='media-desc'>{$desc}</div>";
Try to decouple your code.
I recommend you to take a look to some MVC Framework, in my opinion, the best one is CakePHP.
I wrote this code, it gets an image from a link that varies according to where you are:
<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>
I want to make that code run if a PHP condition proves true, but I cannot make it work. It seems that the function doesn't return a value instead it takes the link textually. I mean it goes to http://chusmix.com/Imagenes/grupos/.jpg literally. However the code works correctly by itself.
This is the PHP code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
}
?>
You are already inside the php tag. So there is no need for <?php and ?>.
Try:
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr($search,1).".jpg'>";
Replace line
echo "<img src='http://chusmix.com/Imagenes/grupos/<?php echo substr(get_search_query(), 1); ?>.jpg'>";
with
echo "<img src='http://chusmix.com/Imagenes/grupos/" . substr(get_search_query(), 1) . ".jpg'>";