At first, I know that using eval() is a bad choice,
but my team already built an application based on templates stored in a database.
I am appointed to edit a template that should use a variable in the script outside the eval() function.
Here is an code example:
template.php
function template($arg)
{
if($arg == "show")
{
$template = str_replace("\\'", "'", addslashes($row['template']));
}
return $template;
}
$row['template'] = "this is template from database that shows {$row[data]}";
$row[data] = {$variable};
show.php
<?php
include 'template.php'
$variable = "thisShouldAppear";
eval("\$var = \"".template("show")."\";");
echo $var;
?>
The output in show.php is:
{$variable}
And it's supposed to show:
thisShouldAppear
NOTE:
If I assign the variable from outside the database it works but I'm limited to including it from the template inside the database.
Example:
<?php
include 'template.php'
$variable = "thisShouldAppear";
$row[data] = "{$variable}";
eval("\$var = \"".template("show")."\";");
echo $var;
?>
In this case it works , but the aim is to have the value from the template not from the script.
Any help would be extremely appreciated, for I'm running out of schedule.
Thank you.
Related
I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.
I had two script, one in .php and one in .tpl
I need to pass the variable in php to the tpl.
I tried this one, but nothinng works (but somehow
it works for one or two days, and after that,
it showed blank,
if i create another php script just to echo the variable, it works.
PHP Code:
<?php
$usdidr2 = "12610.198648";
$usdidr2 = number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>
SMARTY Code:
<li>
<a href="example.php"><strong>
{php}
session_start();
$regValue = $_SESSION['regUSDIDR1'];
$regValue2 = number_format(45.99*$regValue,2,',','.');
echo "Rp. ".$regValue."";
print_r($regValue);
{/php}
</strong></a>
</li>
Here is the syntax to send data from php to tpl
$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);
Update:
$smarty->assign('rate',$usdidr2);// you just need to write rate without $
You can access it in smarty like {$rate} if it is string
You can access it in smarty like {$rate|print_r} if it is array
You can use this syntax:
$res = "Hello World!";
$this->context->smarty->assign('result', $res);
And passing to .tpl file like this:
{$result}
Hope this helping you.
The title says it all: I want the page to determine the title, but the title is being set before the page is being read (I think). Is there a way to accomplish this, or am I doomed to include the header on each individual page?
Here's what I have:
php.ini:
auto_prepend_file = "header.php"
header.php:
<?php
if (isset($title) == false) {
$title = "foobar";
}
$title = "My Site : " . $title;
?>
<title><?php echo($title) ?></title>
index.php
<?php
$title = "Home"; // ideally this would make the title "My Site : Home"
?>
Instead of using auto_prepend_file, I would just use:
include 'header.php';
An important reason why I wouldn't use auto_prepend_file is, if you move to another server, you'll have to remember to edit the php.ini. If you just include the file, you can move your code to any server.
Also, just like Fred-ii- said, I wouldn't use parenthesis. Also, you are missing a semi-colon after the echo.
To take that a step further, I would create a file called something like $config.php or $vars.php. Include that before everything and have it define all your global variables and constants.
I would check this out: http://php.about.com/od/tutorials/ht/template_site.htm
This is not an ideal answer, but I could use CGI variables to get the name of the page, then turn that into a title.
function get_title($page){
$title = str_replace("/", "", $page);
$title = str_replace("_", " ", $title);
$title = str_replace(".php", "", $title);
$title = ucfirst($title);
if($title = "Index"){
$title = "Home";
} elseif ($title == "") {
$title = 'Foobar';
}
return $title;
}
$title = get_title($_SERVER["PHP_SELF"]);
$title = 'My Site: ' . $title;
As a follow-up to my original comment, I'm posting this as an answer because while it doesn't specifically solve the problem, it addresses the underlying cause.
Disclaimer: The code below has many problems, especially security, it's not meant to be copied directly but only explains the concept.
What you need to do is have a container file that includes your headers and whatever else, and each PHP file is included from there. For example, name your container index.php, and have the following in it:
<?php
include 'header.php';
if ($_GET['page'])
include $_GET['page'].'.php';
include 'footer.php';
?>
Then each PHP page you have will be wrapped in the index.php file, and you can add whatever you want in the header file which will be included in all of your files. That way you don't have to include anything in the individual page files.
The client will access your pages with a query string, such as: index.php?page=test
Again, for security reasons you will still want to include basic checks in each individual file, but technically this can be avoided in you plan for this. You definitely won't need to include huge headers in each file, like MySQL connections etc. Also for security you should have stringent checks on your $_GET variables to make sure that only the pages you want can be included.
I'd define a writeTitle (or similar) function in the header.php file which you're auto_prepending:
header.php
<?php
function writeTitle($title = 'foobar') {
$title = "My Site : " . $title;
return '<title>' . $title . </title>';
}
And then you can just call the function from your page scripts instead of setting a variable:
index.php
<?php
echo writeTitle('Home');
I currently have a header.php page which stores all the styles and positions of my header and layout. I then have a contactus.php page which includes the header.php page.
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
I want to alter the style of the link that the person has clicked, so that it pops out indicating to the person which page they are on. Currently, when I try altering the value of $classnamehere inside of contactus.php (by simply assigning it a new value) to change the class printed inside the <a href> tag, nothing happens. It executes the include command, and outputs the value that was stated inside header.php, ignoring my attempts to change the value on the new page.
Is there any way to change the value within contactus.php only, while still keeping the initial value inside header.php so that all the other pages can still use the 'default' style?
Edit: Inside contactus.php, can I change the value of a variable obtained (included) from header.php without actually 'changing' the global variable?
Looking at your code:
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
your code does the following:
contactus.php is executed.
header.php is included, setting the $classnamehere to
'"linkStyleOne"' and creating the actual link with classname
linkStyleOne.
After that $classnamehere is set to linkStyleTwo
This means that you have to assign classname BEFORE including the header.php.
Instead of including it in contactus.php you could do the logic within header.php:
<?php
if ($currentPage) == 'contact') {
//Set this class when user is on a specific page
$classnamehere='"linkStyleTwo"';
}
else {
$classnamehere= '"linkStyleOne"';
}
?>
and just do this in contactus.php
<?php
$currentPage = 'contact';
include 'header.php'
?>
change this
class= <?php echo '"$classnamehere"' ; ?>.
to
class= "<?php echo $classnamehere ; ?>"
Or use it this way
<?php echo '"', $classnamehere ,'"' ; ?>
Or this way(im not sure this works)
<?php echo '"{$classnamehere}"' ; ?>
If you include code, all variables within included code are global and therefore all chnages are reflected in all parts.
in header.php
$a = 10;
in contactus.php
include "header.php"
$a = 0;
And there is no way of changing $a only inside contactus. All code after $a change will be using the new value.
You can create temporary variable $a_copy inside contactus and then change this variable:
$a_copy = $a;
....class= <?php echo '"$a_copy"'...
You should create a PHP class and include it in your header, or make it your header. Invoke it on the page then assign the value for the class variable which you can then echo a separate class function which uses the variable values you require. I can provide an example if you're unfamiliar with the process.
Header.php simple example:
<?php
class Template
{
private $active_nav;
private __construct()
{
}
public function set_active($page = '') // set page test value
{
$this->active_nav = $page;
}
public function get_active() // return page test value
{
return $this->active_nav;
}
public function nav_links() // this could be changed to return an entire header or part or whatever -- change the scope accordingly
{
// active link CSS is linSktyleOne
$current_page = $this->get_active();
$nav_links = '<ul>';
$nav_links .= '<li>Index</li>';
$nav_links .= '<li>Contact</li>';
$nav_links .= '</ul>';
return $nav_links;
}
}
contact.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('contact');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
index.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('index');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
Does all that help make more sense? You only add 1 line of code to each page, and if you really wanted to get fancy you could remove the set function invocation on each page by using a substring of the$_SERVER['REQUEST_URI'] value for a conditional test and put the condition to set the value in the constructor of the class.
I wrote a php page which has two php tags and one script tag inside it .
<?php
$value = $_GET['hash'];
?>
<script>
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
echo $cmd;}
?>
}
</script>
I want to use $value inside another php tag ( like above it has the file I want to open ), but I am not able to do it.Is the scope of variable limited to one php tag ? if yes how can I solve this problem Please help
Your code works perfectly. The variables in one PHP tag is accessible from all other tags, unless you define them inside a PHP function.
The reason you are not seeing the echo on the screen is because the echo prints to the Javascript function.
If you view the source of the generated page, the file contents will be there.
Try this:
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
?>
alert( <?php echo $cmd; ?> );
<?php
}
?>
}
execute();
if $value is a get then you don't need to access it as a file, it should just be a short string.
just above line 7 (the one with $readfile = file...
type:
echo "alert(The hash value is: ".$value.")";
This will make an alert display (as it is in a script tag)
p.s you should have in your opening tag