I would like to pass the value of a php variable using the URL, by using the variable name. Here is a sample of what I am trying to achieve, it doesn't work, bu also doesn't crash so I know that I am close....anyone...please:
Unit Learning
Personally I don't like to put little pieces of PHP code inside HTML. It can be done, but it is problematic. Better to write clean PHP code, like:
<?php
$url = 'student_what_learning_unit.php?student='.urlencode($student_user);
echo 'Unit Learning';
You do need the urlencode() for an URL, see:
http://php.net/manual/en/function.urlencode.php
Just add an echo like this:
Unit Learning
Related
I'm a little unsure of how to word this one but essentially, I want to achieve the following:
http://my.website/?url=http://another.website/?var1=data&var2=moredata&id=119
And for the URL variable to be: http://another.website/?var1=data&var2=moredata&id=119
Naturally, PHP sees var2 and id as new variables. This would be used to pass a full URL from one page to another, however, it poses an issue when the page already has its own variables in the URL!
Any help appreciated!
You need to encode the secondary url which you're putting inside the url variable when you create it. This will ensure it doesn't contain special querystring characters that the receiving website will misunderstand. If the code in my.website is PHP too then the urlencode function (http://php.net/manual/en/function.urlencode.php) is your friend. For example:
urlencode("http://another.website/?var1=data&var2=moredata&id=119")
produces
http%3A%2F%2Fanother.website%2F%3Fvar1%3Ddata%26var2%3Dmoredata%26id%3D119
which will not be misunderstood by the PHP code reading it as containing further separate variables.
I have a settings page in my Wordpress Admin Panel where I save some HTML code(with some PHP code in it) as a Wordpress Option, using update_option.
In phpmyadmin, the value is stored exactly like this:
<img src = \"<?php bloginfo(\'template_directory\'); ?>/images/flexslider/phone.png\">
It works perfect until I try to actually make the code work in a page. I'm printing it like this:
<?php echo urldecode(get_option('wp_slider_code')); ?>
This, unfortunately, prints the PHP code as it was HTML code. So the PHP code doesn't actually get executed; it's treated like a text, the url becoming:
<?php bloginfo('template_directory'); ?>/images/flexslider/phone.png
What can I do to make this PHP code get executed when I echo it on a page?
You have to use the eval() built-in function:
eval( $YourString );
(Edit:) If $YourString return a result, to cath the result you have to use:
$result = eval( $YourString );
Please note:
Caution
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
Read mor on PHP Documentation.
Say I have a url like this in a php variable:
$url = "http://mywebsite.extension/names/level/etc/page/x";
how would I automatically remove everything after the .com (or other extension) and before /page/2?
Basically I would like every url that could be in $url to become http://mywebsite.extension/page/x
Is there a way to do this in php? :s
thanks for your help guys!
I think parse_url() is the function you're looking for. You can use it to break down an URL into it's component parts, and then put it back together however you want, adding in your own things as needed.
As PeeHaa noted, explode() will be useful for dividing up the path.
I'm looking for something that Is really hard for me to do.. I really tried to search all over the net for Solution, But I couldn't seem to find any. I also tried doing this for hours.
What I'm doing: Making a theme for PHPBB2, Installed a MOD that can include PHP in themes.
What is the problem: When I'm doing {} tags in php, It just can't echo those tags.
Let's say I have a function that creates a Table for me, like that:
CreateMyTable(Name,Size,Color);
I put in the function those strings:
CreateMyTable("{FORUM_NAME}",1000,red);
The title stays blank, I actually want it to echo {FORUM_NAME}.
How can I do this?
P.S: I can't do this
CreateMyTable(?>{FORUM_NAME}<?php , 1000, red);
It's not going to work becuase <? = <!-- PHP --> , ?> = <!-- ENDPHP -->.
Thanks for your help :)
If you look in the PHPbb2 template class, you'll find that the template is simply an evaluated set of PHP using the eval() function. You can either print the contents of the PHP before it is parsed using eval() and then use the variable name that the template gives, IE something like (which may not work depending how your template is setup):
CreateMyTable(((isset($this->_tpldata['.'][0]['FORUM_NAME'])) ? $this->_tpldata['.'][0]['FORUM_NAME'] : '' ),1000,randomcolor());
Please note, in order to do it similar to the way above you'd actually have to insert this into your template class.
An much better solution is to avoid using the mod that allows PHP in templates and use JavaScript in the templates to create the function, then print a call to that JavaScript function.
This will work:
CreateMyTable(FORUM_NAME,1000,red);
I also noticed that red is used without quotes - is this also a constant? If it's a variable it needs to have a $ in front of it. If it's a string it should be between quotes.
CreateMyTable(FORUM_NAME,1000,"red");
I have a string that has HTML & PHP in it, when I pull the string from the database, it is echo'd to screen, but the PHP code doesn't display. The string looks like this:
$string = 'Hello <?php echo 'World';?>';
echo $string;
Output
Hello
Source Code
Hello <?php echo 'World';?>
When I look in the source code, I can see the php line there. So what I need to do is eval() just the php segment that is in the string.
One thing to consider is that the PHP could be located anywhere in the string at any given time.
* Just to clarify, my PHP config is correct, this is a case of some PHP being dumped from the database and not rendering, because I am echo'ing a variable with the PHP code in it, it fails to run. *
Thanks again for any help I may receive.
$str = "Hello
<?php echo 'World';?>";
$matches = array();
preg_match('/<\?php (.+) \?>/x', $str, $matches);
eval($matches[1]);
This will work, but like others have and will suggest, this is a terrible idea. Your application architecture should never revolve around storing code in the database.
Most simply, if you have pages that always need to display strings, store those strings in the database, not code to produce them. Real world data is more complicated than this, but must always be properly modelled in the database.
Edit: Would need adapting with preg_replace_callback to remove the source/interpolate correctly.
You shouldn't eval the php code, just run it. It's need to be php interpreter installed, and apache+php properly configured. Then this .php file should output Hello World.
Answer to the edit:
Use preg_replace_callback to get the php part, eval it, replace the input to the output, then echo it.
But. If you should eval things come from database, i'm almost sure, it's a design error.
eval() should work fine, as long as the code is proper PHP and ends with a semicolon. How about you strip off the php tag first, then eval it.
The following example was tested and works:
<?php
$db_result = "<?php echo 'World';?>";
$stripped_code = str_replace('?>', '', str_replace('<?php', '', $db_result));
eval($stripped_code);
?>
Just make sure that whatever you retrieve from the db has been properly sanitized first, since you're essentially allowing anyone who can get content into the db, to execute code.