How to close double include_once in php $div - php

I am including two files in my index page:
$PageData = new Page_Data();
$PageData->title ="Welcome to my page";
$div= "<div class='login-form'>" . include_once "views/navigation.php" . include_once "controllers/login.php" . "</div>";
$PageData->content .=$div;
And I get these two errors.
Warning: include_once(controllers/login.php): failed to open
stream: No such file or directory in
C:\Users\kalle_000\Documents\KEA\3rd Semester\3rd
Module\Module_8\PHP_new\index.php on line 9
Warning: include_once(): Failed opening 'controllers/login.php'
for inclusion (include_path='.;C:\php\pear') in
C:\Users\kalle_000\Documents\KEA\3rd Semester\3rd
Module\Module_8\PHP_new\index.php on line 9 test content
What am I doing wrong? All the files are in the right folder where they should be. Any help appreciated!

include_once "views/navigation.php" . include_once "controllers/login.php" . "</div>"
This is basically evaluated as
include_once ("views/navigation.php" . (include_once ("controllers/login.php" . "</div>")))
That means first "controllers/login.php" . "</div>" is tried to be included, then views/navigation.php0 ("views/navigation.php" + result of the first include). That obviously won't work very well.
Further, the included content probably won't be concatenated into $div either, but output directly.
You probably want:
ob_start();
echo "<div class='login-form'>";
include_once "views/navigation.php";
include_once "controllers/login.php";
echo "</div>";
$div = ob_get_clean();

I changed the syntax to this:
$PageData = new Page_Data();
$PageData->title ="Welcome to my page";
$div= "<div class='login-form'>";
$div.= include_once ("views/navigation.php");
$div.= include_once ("controllers/login.php");
$div.= "</div>";
$PageData->content .=$div;
For some reason, the browser didn't understand the previous syntax, but gets this and now it works perfectly. Thanks for the help.

Related

Get Array from PHP file and use in another PHP file

So i am having trouble getting an array from one PHP file to another.
In my first file (file1.php) i got following code:
print_r($order->delivery);
It will get visible when using echo of course, and it outputs the right things. It gives me an array with order information. Now i got another PHP file I need to use this information in.
What i tried so far is including file1.php to file2.php and then echo the array... But the result is empty.
require_once('path/file1.php');
echo print_r($order->delivery);
And i tried echo my array directly in file1.php adding a div like this
echo "<div id='test'>" . print_r($order->delivery, true) . "</div>";
And then getting the inner HTMl of the div with DOM
$dom = new DOMDocument();
$dom->loadHTML("mypageurl");
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//div[id="test"]');
if($divContent->length > 0) {
$node = $divContent->item(0);
echo "{$node->nodeName} - {$node->nodeValue}";
}
else {
// empty result set
}
Well... none of it works. Any suggestions?
You have to set a variable or something, not echoing it.
file1.php:
$delivery = $order->delivery;
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";
Or you use the $object directly if it is set in file1.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page
Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php
And try this code please:
require_once('path/file1.php');
global $order;
print_r($order->delivery);
Defining $order as global should fix your issue.
You could return an array in a file and use it in another like this:
<?php
/* File1.php */
return array(
0,1,2,3
);
-
<?php
/* File2.php */
var_dump(require_once('File1.php'));

include_once inside echo .. almost got it but i will need some help pls

I'm trying to insert a include_once inseide a echo do_shortcode and my line is this:
<?php $activar = get_option("activar-pelicula"); ?>
<?php
if ($activar == "true") {
echo do_shortcode('[to_like]' .include_once "includes/single/player.php"; . '[/to_like]');
} ?>
this is the error I'm getting:
Warning: include_once(includes/single/player.php[/to_like]): failed to
open stream: No such file or directory in
/home/novosfil/public_html/wp-content/themes/grifus/single.php on line
8
Warning: include_once(): Failed opening
'includes/single/player.php[/to_like]' for inclusion
(include_path='.:/usr/lib/php:/usr/local/lib/php') in
/home/novosfil/public_html/wp-content/themes/grifus/single.php on line
8
I'm trying to get this line (original line)
<?php
$activar = get_option("activar-pelicula");
if ($activar == "true") {
include_once "includes/single/player.php";
} ?>
inside the locker with this
<?php
echo do_shortcode('[to_like]' HER GOES THAT PART OF THE PAGE '[/to_like]');
} ?>
ok that's it.
you can try this way:
<?php $activar = get_option("activar-pelicula"); ?>
<?php
if ($activar == "true") {
ob_start();
include_once "includes/single/player.php";
$out1 = ob_get_contents();
ob_clean();
echo do_shortcode('[to_like]' . $out1 . '[/to_like]');
} ?>

file_get_contents with a encoded url

I am having some trouble with a url I encoded, I am getting this error and I don't know how to fix it.
Warning: file_get_contents(%27http%3A%2F%2Fapi.steampowered.com%2FISteamUser%2FGetPlayerSummaries%2Fv0002%2F%3Fkey%3D%27+..+%27%26steamids%3D%27+..) [function.file-get-contents]: failed to open stream: No such file or directory.
Is it not looking outside my home directory? Anyone know how to make the url function?
Any help would be awesome!
<?php
$url= "'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' .$apikey. '&steamids=' .$keyword.";
$string=file_get_contents(urlencode($url));
$json_a=json_decode($string,true);
$json_o=json_decode($string);
//array method
foreach($json_a[personaname] as $p)
{
echo '
<p>
Name: '.$p[response][players][personaname].'
';
}
?>
Assign your URL enclosed in only one type of quotes:
$url= "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" . $apikey . '&steamids=' . $keyword;

echo name of file 'including' not 'included'

Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>

Echo entire pre-compiled php page

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');

Categories