PHP, Eval code result in a variable - php

I am using eval to execute php code that is coming from a database. The code is for meta tags and page title. One example looks like this (this is stored in a table):
$name . " test page title |".SiteSettings::getSiteNameAlternate();
And then in the controller I do this ($seo is an object that has the meta_title, and that element has the exact string from the db):
$pageTitle = isset($seo->meta_title) ? #eval("echo $seo->meta_title") : null;
I am using Larevel framework and I pass that page title to the blade template, and this works. The blade code
<title>{{ isset($page_title) ? $page_title : SiteSettings::defaultPageTitle() }}</title>
The issue is because the echo in the eval I get the page title on top of the page also.
I tried using print (dumb I know) and printf. Did not work. Any idea how to put the value of that eval in a variable without output on the screen?

$pageTitle = isset($seo->meta_title) ? #eval("return $seo->meta_title;") : null;
it will assign $seo->meta_title to $pageTitle if $seo->meta_title is set, and null otherwise.

A good way to store results is with ob_start, ob_get_contents and ob_end_clean
/*start buffering;*/
ob_start();
eval(echo isset($seo->meta_title) ? $seo->meta_title : "");
/* now catch the Buffer of the eval */
$pageTitle = ob_get_contents();
/* pageTitle contains now the value of the buffered echo */
/* stop buffering; */
ob_end_clean();
now use the Controller as you wanted

If I can understand your question correctly, the issue appears because you did an echo inside eval which will then executed. So instead, you should store the value of $seo->meta_title to variable and then let the view echo it.
In your controller, store meta title value as page title if isset, unless null
$pageTitle = null;
if (isset($seo->meta_title)) {
$metaTitle = '';
eval("\$metaTitle = \"$seo->meta_title\";");
$pageTitle = $metaTitle;
}
Then in your view
<title>{{ isset($page_title) ? $page_title : SiteSettings::defaultPageTitle() }}</title>

Related

Variable auto_prepend_file?

Okay, I suck at titling my questions XD If anyone has a better title, please edit!
Anyway, I have this in my php.ini file:
auto_prepend_file = ./include/startup.php
auto_append_file = ./include/shutdown.php
startup.php looks something like this:
<?php
chdir(__DIR__."/..");
require("include/db.php");
// more setup stuff here
if( $_SERVER['REQUEST_METHOD'] == "AJAX") { // yup, custom HTTP method :p
// parse file_get_contents("php://input")
}
else {
$output_footer = true;
require("include/header.php");
}
shutdown.php goes something like this:
<?php
if( isset($output_footer)) require("include/footer.php");
Now, on to the main problem.
header.php includes the line <title>My shiny new site</title>. I want that title to depend on the page that is currently being viewed. At the moment, I have some ugly hack that involves JavaScript:
document.title = document.getElementsByTagName('h1')[0].textContent;
Obviously, this is not ideal! Any suggestions on how to adjust the title when said title is being output as a result of an auto_prepend_file?
I don't think you should use the append and prepend functionality like that But that's just my opinion.
Why not create header.php to allow for a variable title text. And drop the prepend and append scripts completely
<title><?php echo (isset($title)) ? $title : 'default title'; ?></title>
And whenever you need a title tag.
$title = "some title";
require("include/header.php");

Execute PHP code in a String without Eval

Currently developing a "simple" template class, the problem is how would I execute PHP code within a string without using eval?
A following example is how my template class works:
$user = 'Dave';
ob_start();
include 'index.tpl';
$content = ob_get_clean(); // String
$pattern = sprintf('/%s\s*(.+?)\s*%s/s', '{{', '}}'); // replace with php tags
$new_content = preg_replace($pattern, '<?php echo $1; ?>', $content);
echo $new_content;
index.tpl
<html>
<head></head>
<body>
Hello {{ $user }}!
</body>
</html>
I get the following result:
Hello !
I don't want to use eval because how slow and bad it is to use, is there any other way of doing this? laravel blade engine does not use eval so there must be.
Thanks,
Joel.
You don't need to execute PHP Code. You replace your {{ $user }} with PHP code, which doesn't get executed anymore. So your HTML will look like this after the replace:
<?php echo "Dave" ?>
Your Browser thinks <?...> is an HTML-tag and thus doesn't display the correct name.
Solution:
Just replace {{ $user }} with Dave, why do you want to add more PHP Code?
I suggest to you when you assign a value to a variable, you should put it as a global variable like this;
$GLOBALS['My_Vars']['VarName'] = $Value;
when you retrevie the name of the variable from your code which is in your example $user, you change {{ $user }} to the value within $GLOBALS['My_Vars']['user']
in this case you don't need to use evel

Change title in browser tab

I'm trying to change the title in browser tab for a child theme of twentytwelve. I wan't it to print out the same title on every page. What the heck is happening?
<?php
function im_awesome_title($title){
$title = "Im awesome!";
return $title;
}
add_filter( 'wp_title', 'im_awesome_title', 20 );
or some like this. script better put in the end of all php tags
<?php
//set title
echo "<script>setTimeout(function(){var tts = document.getElementsByTagName(\"title\");if(tts.length > 0)tts[0].innerHTML=\"My title\"; else {var tt0 = document.createElement(\"TITLE\"); tt0.innerHTML = \"My title\"; document.head.appendChild(tt0);}}, 200);</script>";
?>
Your im_awesome_title($title) function will always return the value of $title when invoked. To print this title on every page, you will need to call the function in the title meta tag of your pages. So it will look like this:
<head>
<title><?php echo im_awesome_title($title); ?></title>
</head>
Of course your function expects an argument, so if you want the same title on every page, it's best to leave out the argument and simply set your $title variable to whatever page title that you want to use on all your pages.
Hope this helps.

Smarty:How can Use Php code get the foreach's variable

My Code:
<!-- {foreach from=$goods_cat.cat_id item=rec_cat name=f1}-->
<?php
**echo $rec_cat[id]; // get nothing, why?**
$smarty->assign('goods_cat_' . $rec_cat[id], assign_cat_goods($rec_cat[id], 4));
$smarty ->assign('cat_goods_nf' , 'goods_cat_' . $rec_cat[id]);
?>
<!--{foreach from=$cat_goods_nf item=goods}-->
{$goods.url}
<!--{/foreach}-->
<!--{/foreach}-->
I need the id of rec_cat ,so I use PHP Tags to get it,but Its display nothing? why ?How can I correct it?
Don't use <?php tag inside a template, but instead use {php} tag of Smarty :
http://www.smarty.net/docsv2/fr/language.function.php.tpl
Then you get your variables just like so :
$var = $this->get_template_vars('var');

Add to page title tag based on variable from URL

I have seen the following thread but it's a bit beyond me...
How can I change the <title> tag dynamically in php based on the URL values
Basically, I have a page index.php (no php in it just named to future proof - maybe now!). It contains numerous lightbox style galleries which can be triggered from an external link by a variable in the URL - e.g. index.php?open=true2, index.php?open=true3, etc.
I would like the index.php title tag - to include existing static data + append additional words based on the URL variable - e.g. if URL open=true2 add "car gallery", if URL open=true3 add "cat gallery", if URL has no variable append nothing to title.
Can anyone assist? I have been searching but either missed the point of posts or it hasn't been covered (to my amateaur level).
Many thanks. Paul.
At the top of your php script put this:
<?php
# define your titles
$titles = array('true2' => 'Car Gallery', 'true3' => 'Cat Gallery');
# if the 'open' var is set then get the appropriate title from the $titles array
# otherwise set to empty string.
$title = (isset($_GET['open']) ? ' - '.$titles[$_GET['open']] : '');
?>
And then use this to include your custom title:
<title>Pauls Great Site<?php echo htmlentities($title); ?></title>
<title>Your Static Stuff <?php echo $your_dyamic_stuff;?></title>
<?php
if( array_key_exists('open', $_GET) ){
$title = $_GET['open'];
}else{
$title = '';
}
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
The content of the document......
</body>
</html>
http://www.w3schools.com/TAGS/tag_title.asp
http://php.net/manual/en/reserved.variables.get.php
PHP can fetch information from the URL querystring (www.yoursite.com?page=1&cat=dog etc). You need to fetch that information, make sure it's not malicious, and then you could insert it into the title. Here's a simple example - for your application, make sure you sanitise the data and check it isn't malicious:
<?php
$open = "";
// check querystring exists
if (isset($_GET['open'])) {
// if it does, assign it to variable
$open = $_GET['open'];
}
?>
<html><head><title>This is the title: <?php $open ?></title></head>
PHP has lots of functions for escaping data that might contain nasty stuff - if you look up htmlspecialchars and htmlentities you should find information that will help.
Some of the other answers are open to abuse try this instead:
<?php
if(array_key_exists('open', $_GET)){
$title = $_GET['open'];
} else {
$title = '';
}
$title = strip_tags($title);
?>
<html>
<head>
<title><?php echo htmlentities($title); ?></title>
</head>
<body>
<p>The content of the document......</p>
</body>
</html>
Otherwise as #Ben has mentioned. Define you titles in your PHP first to prevent people from being able to directly inject text into your HTML.

Categories