I have my web site in html, with php chunks embedded. I define some variables in the main page as
<?php
$GLOBALS['myVar']= "something";
?>
later I have
<?php
echo '<p align="center"><img src="./tempImg.php">';
?>
the tempImg php file displays, using the phplot.php lib, a dynamic plot of some data read from the database and in the tempImg.php file I must use the myVar variable.
I tried using the GLOBALS[], the _SESSION[] but I am not able to share the variable in this way.
thanks for any help
$GLOBALS['myVar']= "something";
echo '<p align="center"><img src="./tempImg.php?myVar=' . $GLOBALS['myVar'] . '">';
Or just:
$myVar = "something";
echo '<p align="center"><img src="./tempImg.php?myVar=' . $myVar . '">';
Then access $_GET['myVar'] in tempImg.php.
Related
I have an application that is using session variables. I am trying to pass the path of an image to another PHP page, but since the page is in another location my path breaks and the images won't display. Is there a way to concatenate onto the path in the $_SESSION variable?
page1.php
<?php
session_name('Private');
session_start();
$_SESSION['first_img'] = '<img src="img/first-img.png">';
$_SESSION['second_img'] = '<img src="img/second-img.png">';
?>
page2.php
<?php
session_name('Private');
session_start();
echo $_SESSION['first_img'];
echo $_SESSION['first_img'];
?>
since page2.php is in another folder, I would need to add ../ to the beginning of the src in the image path. I can't figure out how to add it to the $_SESSION variable.
one possible solution might consider storing the relative path in the session variable, and then wrapping the differences in your echo code.
page1.php
<?php
session_name('Private');
session_start();
$_SESSION['first_img'] = 'img/first-img.png';
$_SESSION['second_img'] = 'img/second-img.png';
?>
page2.php
<?php
session_name('Private');
session_start();
echo '<img src="../' . $_SESSION['first_img'] . '">';
echo '<img src="../' . $_SESSION['first_img'] . '">';
?>
Use absolute paths.
$_SESSION['first_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/first-img.png">';
$_SESSION['second_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/second-img.png">';
refering as previous question here HTML Generator: Convert HTML to PlainText and put in a textbox using PHP
Now i got some problems even if the reply produce the expected result.
I got these 3 pages:
Page1.php
// This page contain two columns, one for the form that take the
variables, and other one that contain the iframe that must to display the plaintext
Page2.php
// Cutted code that take $_GET variables and store in $_SESSION
$html = file_get_contents('page3.php');
echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116" value="'. $html .'"></textarea>';
Page3.php
// This is the file page3.php that must to be in plaintext, but first
it must take the variables from $_SESSION and complete the code
Now I get the plain text file but the variables aren't passed since i've stored them in session. i got $var instead of the value.
And the textbox displays only half of the file, not showing the <link> and the whole <style> tags.
<textarea> does not have value.
You need to echo that variable inside the tags.
$html = "Text here";
echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
"it must take the variables from $_SESSION and complete the code"
Also note that you are using sessions. Make sure the session was started having session_start(); at the top of that page and for any other pages that may be using sessions.
It is required.
http://php.net/manual/en/function.session-start.php
Example:
session_start();
if(isset($_SESSION['var'])){
$_SESSION['var'] = "var";
}
else{
echo "Session is not set.";
}
N.B.: Make sure you are not outputting before header.
Consult the following on Stack if you get a headers sent notice/warning:
How to fix "Headers already sent" error in PHP
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Test example which proved successful, echoing var inside <textarea>:
<?php
session_start();
if(isset($_SESSION['var'])){
$_SESSION['var'] = "var";
$var = $_SESSION['var'];
}
else{
echo "Session is not set.";
}
// $html = "Text here";
$html = $var;
echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
Edit:
Base yourself on the following model to assign GET arrays to sessions arrays.
<?php
session_start();
$_GET ['lb1'] = "lb1";
$lb1 = $_GET ['lb1'];
$_GET ['lb1'] = $_SESSION["lb1"];
$_SESSION["lb1"] = $lb1;
//echo "Hey LB1 " . $lb1;
$lb1_session = $lb1;
$_GET ['lb2'] = "lb2";
$lb2 = $_GET ['lb2'];
$_GET ['lb2'] = $_SESSION["lb2"];
$_SESSION["lb2"] = $lb2;
//echo "Hey LB2" . $lb2;
$lb2_session = $lb2;
$html = $lb1_session . "\n". $lb2_session;
echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
?>
Check GET sessions
check_get_sessions.php
<?php
session_start();
if(isset($_SESSION['lb1'])){
$lb1_session = $_SESSION['lb1'];
echo $lb1_session;
}
if(isset($_SESSION['lb2'])){
$lb2_session = $_SESSION['lb2'];
echo $lb2_session;
}
$html = $lb1_session . "\n". $lb2_session;
echo '<textarea readonly style="border:none;resize:none" rows="50" cols="116">'. $html .'</textarea>';
That's the best I can offer you.
Doing $html = $lb1_session . "\n". $lb2_session; you can use "\n" as seperators between each variable to be echo'd. Or, <br> if you want; the choice is yours.
The above assigns the $html variable to chained variables. You can add the others that may need to be added $lb3, $lb4, $lb5 etc.
Good luck! (buon fortunato)
Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.
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');
Hi I'm trying to get a piece of html to only show on the main page which is http://www.domain.com/ ... I wrote the code below but it doesn't work the HTML is showing regardless of the page, am I missing something
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
?>
<div style="margin:0 auto;">
<div style="float:left">
<?php endif; ?>
First of all - please change
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
into
$hweb = 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$hweb may be initialized somewhere before.
Second:
As long as you request 'http://www.domain.com/somename.php' your if condition will never get executed. REQUEST_URI will always hold '/somename.php' except you use some url rewriting.
Third:
Make sure all calls go to 'http://www.domain.com' and not to 'http://domain.com'. Subdomain configurtaions sometimes are very complicated.
At the risk of getting it wrong again..
Why not initialize a variable in the main file before including the header
<?php
$mainfile = true;
?>
then in the header
<?php
if ($mainfile===true)
....
This way the main file can be called anything and be placed anywhere.
Solution 1:
If the above code is written inside 'http://www.domain.com/index.php' file then it may work fine.
Solution 2:
else make sure that $hewb is set with null value earlier b4 this code so that ".=" would not add extra value b4 'http...'.
Now
$hweb = '';
echo $hweb .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
That is because the the HTML is inline in the php file but outside of the PHP tags. You can simply echo the HTML inside the if.
if ($hweb == 'http://www.domain.com/')
{
echo '<div style="margin:0 auto;">';
echo '<div style="float:left">';
}
or if you have lots of HTML you could do it like this
<?php
ob_start();
?>
<html>
<body>
<p>This HTML only be echoed </p>
</body>
</html>
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
{
ob_end_flush();
}
else
{
ob_end_clean(); // Probably not needed
}
?>