Add php text string to the code - php

I have so stupid issue which I just can't resolve now and I need it so much..
So here's the code:
$siteURL = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/';
$bookmarklet = 'javascript:document.location.href=\'' . $siteURL . '?url=\'+escape(document.location.href)';
it takes things from url, like "domain.com/?url=things"
I need the code to add http:// before "things"
I've tried adding $bookmarklet = 'http://'. $bookmarklete and changing it in various ways but it didn't helped.
Please help me guys!

It looks like line 72 of your full code is using the query string url from the page url going to the variable $url
$url = $_GET['url'];
try changing to:
$url = 'http://'.$_GET['url'];
Are you trying to change this bit of the code? You question is not very clear on what needs to be changed (what is it now Vs what you want it to be) or where your example url is coming from and where in your $bookmarklet string the 'url' part needs to go... if anywhere?
Your actual $bookmarklet string looks ok to me although as I said before it is not used in the script so you can assign it anything you like and I can't imagine it making any difference!
You could tidy the $bookmark string to this:
$bookmarklet = "javascript:location.href='$siteURL?url='+encodeURI(location.href)";
encodeURI() is the newer replacement for escape()

Related

Premium url shortner issue with urlencode replacing & sign with ampersand

As a novice and beginner php learner, I'm using the Code-Canyon Premium URL Shortner script and done 2 days of research. Unfortunately I am unable to resolve my issue.
The url shorten script is urlencoding the API url that it sends to the script, In doing this it is replacing the & symbols with & causing the url to not work correctly on the final destination page.
I have tried to use preg_replace, str_replace and also tried to use urldecode on the destination page but none of these seem to work. Here is my current script:
$makeshort = "http://mywebsite.com/email/quote.php?quoteid=$visitor&customertype=fhbs";
$mkshrt = str_replace("/&/","%26",$makeshort);
$short = "http://shorturl.com/api?&api=REMOVED&format=text&url=".urlencode($mkshrt);
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res)
$shorturl = $res;
$shorty = json_decode($shorturl);
$shorturl = $shorty->{'short'};
echo $shorturl;
Note: Where you see &format=text in the api url, I have tried to use it with and without the &format=text however this makes no difference what so ever.
I am hoping that there could be a simple and quick way to resolve this issue as I am only passing over 2 variables and its the second variable that is being displayed like this:
mywebsite.com/email/quote.php?quoteid=01234567890&customertype=fhbs
So the customertype variable is the one being messed up due to the amp; symbol.
I sincerely hope someone with the expertise could advise me on the best approach or even a simple way to resolve this issues as I really am at my whits end! MY knowledge is not good enough to research the exact key phrases in order to point myself in the right direction.
Thanks for your time in reading this and I hope someone would be kind enough to help me out here.
I know the feeling as i myself am just becoming to terms with coding and developing.
I personally would solve this by one of two ways, If you have tried to already use htmlspecialchars or htmlentities along with urldecode then the most simple and quickest way to achieve this would be to read the URL string then replace the &symbol with the & using str_replace and do either a meta refresh of the page or `header location redirect
Here is what i mean with a breif example however one must stress that some extra security maybe needed and this is ONLY a quick fix not a secure stable and permanent fix, Though one could play with this and maybe work something out for your own circumstances.
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
echo "<meta http-equiv='refresh' content='0;URL=$url'>";
exit;
}
Alternative way with header location:
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
header("Location: $url");
exit();
}
This will totally remove any & symbols from the url and replace them with &.
You can also play around with this to remove even more from the url string and replace things like / or forbidden words.
An example of the output will look like this:
Original url causing the problems:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
New url after the script has executed and refreshed the page:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
As you can see from the hyperlinked text above, The ampersand breaks the string and everything after that is not read correctly but when this script executes and refreshes the page the url will be just like the second hyperlink thus making the url work for what you require.
NOTE: THIS IS NOT A SECURE WAY OF DOING THINGS AND MAY NO BE IDEAL FOR YOUR CIRCUMSTANCES, THIS IS JUST AN IDEA AND HOPE THIS HELPS!
Thanks.

Encoding URL partially in Javascript and PHP (url tweak)

that might be something strange when I say that I want to encode a URL partially, But I am caught in this situation and it seems that I got no other solution...
here is my code snippet
$url = isset($_GET['u']) ? esc_url($_GET['u']) : '';
$image = isset($_GET['i']) ? $_GET['i'] : '';
maybeappend = '<a href="?ajax=photo_thickbox&i=' + encodeURIComponent(img.src) +
'&u=<?php echo urlencode($url); ?>&height=400&width=500" title=""
class="thickbox"><img src="' + img.src + '" ' + img_attr + '/></a>';
its taken from wordpress /wp-admin/press-this.php
the issue is, I cant post to my site via Press this bookmarklet
I searched the google, studied the wordpress forums and found that I need to tweek Press this button in my bookmark tool bar in broswer..
but for me, that is not a solution,,why? obviously, I cant teach every visitor to do this change in their broswer..so I have to edit my code residing on server...
how can I edit encodeURIComponent(img.src) and <?php echo urlencode($url); ?> so it DOES NOT ENCODE HTTP:// part of url,
say I got a url 'http://www.google.com I want it to be encoded as www.google.com
any suggesion?
How can I achieve my goal? might be some regex ? (dont know regex :( )
what would be the code adjustment for this??
thanks for your help..
First remove the http part using substr, then apply urlencode and then reattacht the http part.
Same method can be applied in JavaScript using the equivalent JavaScript functions.
$encoded = 'http://' . urlencode(substr($url, -7));
You could indeed use a regex to filter the http:// part of the url (which is, if I understood well, what you want to do).
Something like https?://(.*) should select the part of the url you want (the s? part takes into account potential secure links).
You'll want to make sure any matching strings are at the beginning of the strings
Javascript
Use String.replace with regex:
url.replace( /^(http|https):\/\//, '' );
^ means start of the string
PHP
Use str_replace(): php.net/str_replace
if( strpos( $url, 'http' ) == 0 ){
str_replace( array( 'http://', 'https://'), '', $url, 1 );
}
strpos can also return a FALSE so make sure you use ==.

Small issue with str_replace

This is my code:
$produrl = '/'. basename(str_replace(".html","",$_cat->getUrl())) .'/' . basename($_product->getProductUrl()) ;
$_cat->getUrl() returns this:
http://website.com/category/subcategory.html
I need: for $produrl part of basename(str_replace(".html","",$_cat->getUrl()))to return category/subcategory
The issue:
It gives back only category without /subcategory I know the issue is in the str_replace it's wrong isn't? Can you help me with that?
Pleast Note I have 2 Cases:
Sometimes I get http://website.com/category/subcategory.html.
Sometimes http://website.com/category.html
And I would like it to work with both of them :)
You should use parse_url instead:
$url = parse_url($_cat->getUrl());
$produrl = str_replace(".html","",$url['path']);
The problem is that you're using the basename() function.
Take a look at the documentation for it: http://php.net/manual/en/function.basename.php
It will only return the trailing string after the "/" (forward slash) or "\" (back slash, for Windows).

Correct syntax for php inside a feed request

I have a very basic query string which passes a ID to a receiving page.
On that page, I need to dynamically call the YouTube API, giving my playlistID.
I'm having to use PHP for this, and it's a little out of my comfort zone, so hopefully someone can wade in with a quick fix for me.
Here is my variable
$playlist;
And I need to replace the 77DC230FBBCE4D58 below with that variable.
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/77DC230FBBCE4D58?v=2';
Any help, as always, greatly appreciated!
Once the $playlist variable is set you can construct the feed URL as :
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
or
$feedURL = "http://gdata.youtube.com/feeds/api/playlists/$playlist?v=2";
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/'.rawurlencode($playlist).'?v=2';
Or perhaps a little neater:
$feedurl = sprintf(
'http://gdata.youtube.com/feeds/api/playlists/%s?v=2',
rawurlencode($playlist)
);
(Note: rawurlencode is used just in case [not that it's likely with YouTube playlist IDs] the $playlist value contains any funky characters.)
More infos:
String concatenation with the . operator
Encoding potentially "unsafe" URL characters with rawurlencode
Adding values to "formatted strings" with sprintf
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
you use a full stop to join strings in PHP

str_replace and preg_replace work on one server but not another

UPDATE: As it turns out, the below is caused by a caching issue on my production server. Thanks to everybody who contributed thoughtful answers.
I have a simple function on a php page that takes a url such as:
http://myurl.com/mypage.html?param1=value1
and converts it to:
http://myurl.com/searchpage.html?param1=value1
All it does it swap out the page.html portion.
To do this, I use the following:
$currentUrl = $this->getCurrentUrl(); // Grabs the current url, i.e 'http://myurl.com/mypage.html?param1=value1'
// Derive a search pattern from the current url
$pattern = "/" . str_replace(array("/", ".", "-"), array("\\/", "\\.", "\\-"), $currentUrl) . "/";
// get rid of the 'mypage.html'
$newUrl = preg_replace($pattern, 'http://myurl.com/', $currentUrl);
// replace the question mark with the correct page
$newUrl = str_replace("/?", "/searchpage.html?", $newUrl);
The above code is not the exact code but is a good representation. It works beautifully on one server, but when I push to production, the preg_replace does not work. I originally attempted to use str_replace. It also works on my local development machine, but not on the production server.
I have confirmed that the URL variables are coming in correctly. Any ideas?
That's horribly convoluted (sorry to say). Why not just:
$newUrl = preg_replace('!\bmypage\.html\b!', 'searchpage.html', $oldUrl);
Why don't you just do
$pieces = explode('/',$url);
str_replace('mypage','searchpage',$pieces[2]);
$newURL = implode('/',$pieces);
Way better than using regexps.

Categories