Php missing url-encoding - php

I have the following code:
$markupimage = substr($product_info['image'],0,strrpos($product_info['image'],".")) . "-" . $popw . "x" . $poph . substr($product_info['image'],strrpos($product_info['image'],"."));
this currently outputs with $markupimage =:
http://shop.example.com/image/cache/data/NEW WEBSITE/Products/Hangers/Tees/Full Body/front_black-1000x1000.jpg
Where the 'NEW WEBSITE' text is, the php has missed out the url encoding. It should be NEW20%WEBSITE. How can I alter the php script to include this url encoding?
EDIT:
This is where I use this php script:
$this->document->setFBOG('og:image', HTTP_SERVER . 'image/cache/' . $markupimage);
I have tried wrapping this in both the rawurlencode() and urlencode()
So far I have tried:
$this->document->setFBOG('og:image', urlencode(HTTP_SERVER . 'image/cache/' . $markupimage));
$this->document->setFBOG('og:image', rawurlencode(HTTP_SERVER . 'image/cache/' . $markupimage));
This results in a link that looks something like:
http://shop.example.com/image/cache/data%2FNEW+WEBSITE%2FProducts%2FHangers%2FTees%2FFull+Body%2Ffront_black-1000x1000.jpg

You can use urlencode() function for that.
urlencode()

Related

PHP using Propel

I am having an issue when I try to post the Join Date on my web page:
Fatal error: Class 'getJoinDate' not found in ********
$newDate = getJoinDate::createFromFormat("l dS F Y", $dateFromDB);
$posts = PostQuery::create()->findPk(1);
echo "<p>", "ID:".$posts->getUserID().", ".$posts->getContent().", ".$posts->getJoinDate()." </p>";'
Please let me know what I must do to display the format.
It's this code:
echo "<p>", "ID:".$posts->getUserID().", ".$posts->getContent().", ".$posts->getJoinDate()." </p>";'
I'm not sure what your commas were supposed to be, but I guess not actual commas and so you have commas where stops should be: echo "<p>", <--
And an apostrophe at the end of the line: </p>";'<--
You should grab a decent IDE that will show errors like this immediately, saves a lot of time. And read your error log as it tells you such errors.
Using double quotes variable names will be expanded, try this:
echo "<p>ID: {$posts->getUserID()} {$posts->getContent()} {$posts->getJoinDate()}</p>";
Or this way:
echo '<p>ID: ' . $posts->getUserID() . ' ' . $posts->getContent() . ' ' . $posts->getJoinDate() . '</p>';

How to correctly write <img src> in php without escaping to HTML

I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>

GET variables of parent window to iframe url

I'm trying to pass on the parent url variables to the variables of the iframe url.
For example if the parent url is:
http://mywebsite.com/autos-zoeken?sessid=s838c7e5c3be0452fc38f4ffb6f307ed7&code=be3f&whsearch_key=6568
The iframe url needs to become:
http://anotherwebsite.com/s838c7e5c3be0452fc38f4ffb6f307ed7/be3f/stock/6568/
The code I'm using now is:
<?php
 $val1 = $_GET[“sessid“];
$val2 = $_GET[“code“];
$val3 = $_GET[“whsearch_key“];
echo "<iframe src='http://anotherwebsite.com/' . $val1 . '/' . $val2 . '/stock/' . $val3 . '/' id='blockrandom' width='1000' height='1200'

scrolling='auto'

frameborder='0'

class='wrapper'>

Your browser doesn't support inline frames.</iframe>";
?>
The result on the website is:
http://anotherwebsite.com/' . . '/' . . '/stock/' . . '/' id='blockrandom1' etc
So the variables aren't being put in the right place in the iframe url
What am I doing wrong here?
this should work fine:
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'> Your browser doesn't support inline frames.</iframe>";

the problem is with all your quote's inside your src, it thinks it needs to stop your src after http://anotherwebsite.com/ and you don't need to use "." inbetween because you used doubleqouote on start, you can just use variables inside doublequotes.
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'>Your browser doesn't support inline frames.</iframe>"; ?>
you don't need to escape your variables
You're not actually concatenating multiple strings, you're just building a single string. Take a look at a simplified example:
"<iframe src='http://anotherwebsite.com/' . $val1"
That's just one string which happens to have a period between some spaces. In order to use the . concatenation operator you need to terminate the string first:
"<iframe src='http://anotherwebsite.com/'" . $val1

How do I get fwrite() to not print to the page?

I'm using fwrite() to write a file, however, every time it runs, it prints all of the information to the webpage it's on and writes to the file, too. I'd rather it not echo it to the page.
Alright, I've retrieved the code. Here it is:
fwrite($data_file, "Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation);
Whenever I run this, it outputs everything in the second place of fwrite() onto the page, so it outputs the follow onto the page, except with the variables replaced for their values:
Confirmation Data: \r\n\r\n Song: " . $input_song . "\r\nFile1: " . $file_var1 . "\r\nFile2: " . $file_var2 . "\r\nFile3: " . $file_var3 . "\r\nFile4: " . $file_var4 . "\r\nExplanation Text: " . $input_explanation
You should post your code, so we can follow it but most probably you are also echoing out the code as HTML in your PHP file and this is why it prints out on the screen.
You said your information is being echoed and not written to file. Perhaps you are using echo instead of using file related functions. However, without access to your code, I can only post how to write information to a file in PHP:
<?php
error_reporting(E_ALL);//display all errors
ini_set('display_errors', true);
$filename = "testfile.txt";//the name of your output file
$fh = fopen($myFile, 'w');//create a filehandler - fh) and open the file in write mode
$string = "ABCDEFGH\n";//test string to write to the file
$myvar = fwrite($fh, $string);
$string = "IJKLMNO\n";//update the string variable with new data
fwrite($fh, $string);//write more data - can be repeated ad -infinitum
fclose($fh);//finally, close the file
But, if you truly want definitive answers, post your code so that we may be able to help you.

div onclick isn't working

I'm trying to get the caption part of my slider to link to the respective article url on my wordpress site. I'm pretty sure I found the section in the plugin code I think needs to be edited, but when I try to do the following:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . onclick="location.href='$url';"</div>";
?>
I get "syntax error, unexpected T_STRING, expecting ',' or ';" showing up in my slider, and the rest of my site not working. I've tried many variations of what I've tried to do here, but I can't seem to find one that works.
Here's the original entire slider code if it's helpful: http://pastebin.com/4nKxXkSa
Your opening/closing of double-quotes is wrong : in PHP, strings must be enclosed in either quotes or double-quotes ; and if you want to put a double-quote in a double-quotes enclosed string, you have to escape it with a \.
For example, you need to open a quote or double-quote before onclick, as this is part of a string.
Also, your onclick should be inside the <div ...> tag, and not between <div> and </div>.
In the end, your PHP code would look a bit like this (I've set up hard-coded values for the variables, to help with testing) :
$sl_caption = 'ID';
$sl_htmlcaption = 'HTML';
$url = "URL";
echo "<div id='"
. $sl_caption
. "' class='nivo-html-caption' onclick=\"location.href='$url'\">"
. $sl_htmlcaption
. "</div>"
;
And you'd get the following HTML as output :
<div id='ID' class='nivo-html-caption' onclick="location.href='URL'" >HTML</div>
Your string quotes are not correctly placed. Try this
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
The problem is what it says - "onclick" isn't a quoted string in your example (T_STRING in PHP). Everything you join together (concatenate) in a string needs to be either a) a string in quotes (single or double), b) something that can be converted to a string or c) a variable/constant/function call.
If you didn't have that error then your current example would also have the "onclick" as the content of the tag, rather than an attribute on the tag. What I think you want is:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption' onclick='location.href=\'" . $url . "\''>" . $sl_htmlcaption . "</div>";
?>
If you want standard HTML attributes then you'd normally use double-quotes, which would give you:
<?php
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . '">' . $sl_htmlcaption . '</div>';
?>
Also, is there any reason why you're using an onclick to set the current location rather than a normal <a href>?
the qoute isn't the only problem. The onclick is also out of the div element's properties. It must be (changes the doubles for singles to make it more clear):
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . ';">' . $sl_htmlcaption . '</div>';
Your quotes are the problem. It should be:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
You needed double quotes to start the string again after the concatenation operator (.), but then you need to escape the double quotes inside this string wit a blackslash so the PHP interpreter won't think the string has ended too soon.
And as Pascal pointed out, the onclick attribute should actually be inside the stating div tag anyway:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'onclick=\"location.href='$url';\">" . $sl_htmlcaption . </div>";
Using interpolation may make things easier for you also. When using double quotes to delimit a string you can insert variables' values using curly brackets ({}) like this:
echo "<div id='{$sl_caption}' class='nivo-html-caption'onclick=\"location.href='{$url}';\">{$sl_htmlcaption}</div>";
This way you only have to open and close the string once (at the beginning and end).
Beware:
You cannot interpolate function calls directly. You can either concatenate like "<p>".strlen($x)."</p>" or store the result in a variable and the interpolate like before; "<p>{$result}</p>".
You'll still have to escape double quotes within the string though.

Categories