I am trying to pass a big url as a string to another php page and fetch it there via $_REQUEST[]
but it's not working.
Here is the code (JavaScript) from first page:
var url="index.php?module=Contacts&return_module=Campaigns&action=UD_DetailView&popuptype=detailview&select=enable&form=EditView&form_submit=false";
var intermediate_url="choose_template.php?variable_url="+url;
window.location=intermediate_url;
The code on other page is:
<?php
echo "url = ".$_REQUEST['variable_url'];
?>
The echo I get on page choose_template.php is this :
url = index.php?module=Contacts
I don't understand. Why does it break at the occurance of first & ?
What should I do to bypass it ?
Try using in js:
url = encodeURIComponent(url);
Than in php:
echo "url = ". urldecode($_REQUEST['variable_url']);
When manipulating url, you should use urlencode and urldecode functions
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php
In javascript:
url = encodeURIComponent(url)
Try using urlencode():
var url="<?php echo urlencode('index.php?module=Contacts&return_module=Campaigns&action=UD_DetailView&popuptype=detailview&select=enable&form=EditView&form_submit=false')?>";
var intermediate_url="choose_template.php??variable_url="+url;
window.location=intermediate_url;
If i understand correctly you what to echo out the full name of the current page: Use this to get it:
function curpage() {
$pu = 'http';
if ($_SERVER["HTTPS"] == "on") {$pu .= "s";}
$pu .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pu .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {$pu .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];}
return $pu;
}
<?php echo "url = ".curpage(); ?>
This will alway work fine!
Related
I am trying to build a language switch for a site of mine, but, as the hosting MUST BE a Windows IIS with PHP, I am not able to get the full URL of the page viewed by the visitor.
Say I am on domain.com/page.php?id=23, what I get using the old fashioned
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
is an empty string.
So I have tried a solution found here on stackoverflow to get the full URL
function getFullUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER['HTTP_X_REWRITE_URL'];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER['HTTP_X_REWRITE_URL'];//$_SERVER["REQUEST_URI"] blank
}
return $pageURL;
}
but still nothing. I get something like: domain.com/page.php? in some cases and doman.com/? in others.
Is there a definitive way to get the full url in that scenario?
Thank you very much.
EDIT
It seems that the thing cannot be done on that server. I solved using a client side work around written in js+jQuery (I used purl plugin)....horrible :) Here it is:
$('.lang').each(function(){
$(this).click(function(e){
e.preventDefault();
var lingua = $(this).find('a').data('lang');
var url = window.location.href;
var urlParts = purl(url);
if (url.indexOf("?") >= 0)
{
var queryString = urlParts.attr('query').split('&');
var langIndex = queryString.indexOf('lang');
if(langIndex > -1)
{
queryString.splice(langIndex,1);
var newUrl = urlParts.attr('protocol')+urlParts.attr('host')+urlParts.attr('path')
window.location = newUrl + '&lang='+lingua;
}
else window.location = url + '&lang='+lingua;
}
else
window.location = url + '?lang='+lingua;
});
});
It seems to work, even if I don't get why if "lang" is already in the query string, the script doesn't recognize it and continues adding &lang every time I click on the links. Sure, it works anyway, but it is horrible to watch a URL like &lang=en&lang=fr&lang=pl&lang=de
This line returns empty
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
because it should be this:
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
You need quotes for the keys....see here in the manual.
having a bit of trouble. Basically I have created some pagination. The problem is each time I click on a page number url it just adds the parameter to the url even if it already exists.
so for instance I land on the page. My url is now example.com/page?pagenum=1, I click the second page so my url is now example.com/page?pagenum=1&pagenum=2. Now it all works fine but as you can imagine is going to get a bit messy so would rather it update the parameter that's already in the URL. I'm currently using the following to get the current page URL:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
and then the Link is something like:
<a href='<?php echo curPageURL(); ?>&pagenum=<?php echo "1"; ?>'> 1 </a>
Update
I have other paremeters in the URL I need to keep, I only need to update 'pagenum'
The problem exists because REQUEST_URI contains both the path and query string, and you're appending a new query string to that every page turn. To extract the path, you could use this code, taken from this answer:
$path = strtok($_SERVER["REQUEST_URI"], '?');
You can then copy existing query string fields, but remove pagenum:
$fields = $_GET;
unset($fields['pagenum']); // remove any existing pagenum value
$path .= '?' . http_build_query($fields); // re-append the query string
You could then use more or less your existing link code:
<a href='<?php echo $path; ?>&pagenum=<?php echo "1"; ?>'> 1 </a>
You can use http_build_query like so:
$all_params = $_GET;
$all_params["page"] = "2";
$link = "page.php?" . http_build_query($all_params); // "page.php?page=2&foo=bar"
I've managed to put together the following script:
<?php
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
//function to get the url of the event!
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/* usage */
$short = make_bitly_url('http://site.com/viewEvent.php?id=2323232','bitlyuser','bitlyapikey','json');
echo 'The short URL is: '.$short . "<br>";
echo "PATH: ". curPageURL();
// returns: http://bit.ly/11Owun
?>
Now this code can produce the a short url of whatever is passed to it. I have a tweet button on my site that I got from twitter developer site. it works in that it posts the fully link of the page it is currently on...so not the shorten version. Now i want when that twitter button is pressed for it produce a short url so that I could share on my site's account. How is that done?
Thank you,
You should be able to just set the data-url option to the bitly url. e.g.
Tweet
I have this code in my HTML:
<h3 id="left">Lorem Ipsum </h3>
<h3 id="right">[Current URL Here]</h3>
I want to display (dynamicly) the current URL inside the <h3> tags. I've been trying to figure it out for a few days, but I'm really a mobile developer, not an HTML developer, so it's proven difficult. I need this for an app I'm working on, so Please go easy on me :)
Thanks in advance.
document.getElementById('right').innerHTML = window.location.href;
If you wanted to do it in PHP, it's a little more involved:
$url = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url .= $_SERVER['HTTP_HOST'] . htmlspecialchars($_SERVER['REQUEST_URI']);
As aronasterling points out, you need to sanitize $_SERVER['REQUEST_URI'] to prevent XSS.
Well, you simply cannot do it in pure HTML.
With javascript, you can go with
<h3 id="right">
<script type="text/javascript">
document.write(location.href);
</script>
</h3>
Otherwise, if you are requesting a page on the server, you should rather have it done in there.
the php code for getting complete url of current page is as follows
<?php
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
echo $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
Reference
if you want to use javascript use the method suggested by #MooGoo
full usage of that script is as follows
<SCRIPT LANGUAGE="JavaScript">
document.getElementById('right').innerHTML = window.location.href;
</SCRIPT>
use this after you declared/defined <h3 id="right">[Current URL Here]</h3>
Hope helpful
<script type="text/javascript">
var segments = window.location.pathname.split('/');
var toDelete = [];
for (var i = 0; i < segments.length; i++) {
if (segments[i].length < 1) {
toDelete.push(i);
}
}
for (var i = 0; i < toDelete.length; i++) {
segments.splice(i, 1);
}
var filename = segments[segments.length - 1];
console.log(filename);
document.write(filename);
</script>
While the JavaScript are what is more common, you could also use Server-Side Includes:
<h3 id="right">
<!--#echo var="SERVER_NAME" -->/<!--#echo var="DOCUMENT_URI" -->
</h3>
instead of SERVER_NAME you can try HTTP_HOST
instead of DOCUMENT_URI you can try REQUEST_URI; one includes the query string, the other doesn't
Php Code:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
<h3 id="right">echo curPageURL();</h3>
Lets say the url is http://example.com/product/3 and I only want to retrieve what is after http://example.com/product/. I found this, it echos the domain but how do I get the three. Its this method reliable? I'm using codeIgniter also.
<?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php
echo curPageURL();
?>
Use $this->uri->segment(n). Documentation for it is here:
http://codeigniter.com/user_guide/libraries/uri.html
For your code, you would use:
$curPageURL = $this->url->segment(2)
Edited: Fixed a bug in the code.
Yes, use the URI class of the codeigniter API.
The base_url is segment zero, so in your case, products would be segment 1 and the id segment 2.
$product_id = $this->uri->segment(2)
As chetan mentioned this is clearly documented in the user guide.
You should use parse_url instead:
http://php.net/manual/es/function.parse-url.php