I have the following code
Donate Now
What I want to do with php is replace the absolute url part with php then just append biography/#donate
so basically generating the url so whatever page the user is on they get sent to www.mysite.com/biography/#donate
I have tried using $_SERVER['DOCUMENT_ROOT'] but just seems to add the link to the existing url
thanks in advance
In such cases i'm using the <base> tag in header.
<base href="http://myabsolute.host/or.subfolder/" />
Then i just use relative links to my anchors, which are resolved to my base url.
I've used parse_url in my example. I hope this is what you're looking for:
<?php
$url = "http://blown-away.org/biography/#donate";
$mySiteHost = "mysite.com";
$parts = parse_url($url);
echo str_replace($parts["host"], $mySiteHost, $url)
?>
$url = 'http://blown-away.org/biography/#donate';
echo "Original Url : " . $url . "</br></br>";
$tempArray = parse_url($url);
$tempArray['host'] = "www.mysite.com";
$tempUrlString = "";
foreach($tempArray as $key => $value){
if($key == "scheme"){
$tempUrlString = $tempUrlString . $value . "://";
}else{
$tempUrlString = $tempUrlString . $value;
}
}
echo "Modified Url : " . $tempUrlString . "";
You can use $_SERVER["SERVER_NAME"]; instead
Related
This is how I get my url on my localhost:
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
echo $url;
it returns : http://localhost/CodeSensei/menu because I am on the menu page.
How can I trim this? I only want "http://localhost/CodeSensei"
I know I can trim it like this
echo trim($url,"menu");
But the problem is, that "menu" is dynamic, it keep changing dependes on the page. Is there any way to trim my url so it will always and only print "http://localhost/CodeSensei" in any page?
There are many ways to achieve this. You can play around with different string manipulators like explode() etc.
Here is a solution using explode()
$variable = "http://localhost/CodeSensei/menu";
$variable = (explode("/",$variable));
$url='';
for($i=2;$i<count($variable)-1;$i++)
{
$url .= "/".$variable[$i];
}
$final_url = "http:/".$url;
echo $final_url;
Your output
http://localhost/CodeSensei
This function may help you. It get a url and return the url without after the last /.
<?php
function getSubUrl($originUrl) {
$url = parse_url($originUrl);
$url['scheme'] .= '://';
$url['path'] = dirname($url['path']);
return implode($url);
}
echo getSubUrl('http://localhost/CodeSensei/menu/123') . PHP_EOL;
// http://localhost/CodeSensei/menu
echo getSubUrl('http://localhost/CodeSensei/menu') . PHP_EOL;
// http://localhost/CodeSensei
echo getSubUrl('http://localhost/CodeSensei') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost/') . PHP_EOL;
// http://localhost/
echo getSubUrl('http://localhost') . PHP_EOL;
// http://localhost
In my prestashop shop i have fetched the current web page url by using the below php code.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
My current echo url is http://shoppingworld.com/int/Mens-Tshirts/Fashion.html
My shop url is http://shoppingworld.com/int/
I need to remove the url portion which is coming next to the above shop url.
Try this
<?php
$url_path="http://www.shoppingworld.com/int/Mens-Tshirts/Fashion.html";
$a = parse_url($url_path, PHP_URL_SCHEME);
$b = parse_url($url_path, PHP_URL_HOST);
$url_name_parse=explode('/',$url_path);
$url_name=$url_name_parse[3];
echo ($a . "://" . $b .'/' .$url_name.'/'); ?>
Program Output
http://www.shoppingworld.com/int/
DEMO
You can't directly get that partial url.
Try this,
$url = 'http://shoppingworld.com/int/Mens-Tshirts/Fashion.html';
$parsed = parse_url($url);
$path_array = explode('/', $parsed['path']);
echo $parsed['scheme'] . '//' . $parsed['host'] .'/'. $path_array[1] . '/';
Demo
$arr_url = parse_url($url);
$host = $arr_url['host'];
$service_uri = $arr_url['path'];
read more in php manual about parse_url();
I have an example link here:
http://mydomain.com/myapp1/index.php/image/index/album/check+picture/id/1
Now I'm trying to retrieve the "1" at the very end of the url.
So far, I've tried the following code:
$id = $_GET['id'];
But it is not working. I was used to the url having the index.php?id=1 Syntax but I'm not entirely sure how to get this one working.
UPDATE
Before accepting an answer, I wanted to add this script I used to get the entire URL of the current page:
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
echo $currentUrl;
When it echoes, it only prints out:
http://www.mydomain.com/myapp1/index.php?
Just do this :
echo basename($_SERVER['REQUEST_URI']);
Something like this should work for you:
$id = substr($url, strrpos( $url, '/' )+1);
Get YII Documentation...
$id = Yii::app()->request->getQuery('id')
Or
$id = Yii::app()->getRequest()->getQuery('id');
Possible duplicate
http://stackoverflow.com/questions/2760869/yii-framework-controller-action-url-parameters
try this....
$url = "http://mydomain.com/myapp1/index.php/image/index/album/check+picture/id/1";
$value = substr(strrchr(rtrim($url, '/'), '/'), 1);
Ref
In Yii Framework you can get value like this...
$id = Yii::app()->getRequest()->getQuery('id');
OR
$id = Yii::app()->request->getParam('id');
My url contains many variables that I want untouched (don't worry they aren't important).
Let's say it contained...
../index.php?id=5
How would I make a url that just adds
¤t=1
rather than replacing it entirely?
I'd like...
../index.php?id=5¤t=1
rather than..
../index.php?current=1
I know it's a simple question but that's why I can't figure it out.
Thanks.
To append a parameter to a URL you can do this:
function addParam( $url, $param ){
if( strrpos( $url, '?' ) === false){
$url .= '?' . $param;
} else {
$url .= '&' . $param;
}
return $url;
}
$url = "../index.php?id=5";
$url = addParam( $url, "current=1");
You should just create your link to 'add' that parameter
The Link
and then obviously in the index.php somewhere you'll look for the current variable and do what you need to:
<?php
if(isset($_GET['current']) && !empty($_GET['current]) {
// Do stuff here for the 'current' variable
$current = trim($_GET['current']);
}
?>
On the links that you require the $current variable, I suppose that you could just casually put it in the href attribute. For the index,php file, so something like this....
if(isset($_GET['current']))
{
$current = $_GET['current'];
//Do the rest of what you need to do with this variable
}
Try this one:
$givenVar = "";
foreach($_GET as $key=>$val){
$givenVar .= "&".$key."=".$val;
}
$var = "&num=1";
$link = "?".$givenVar."".$var;
echo $link;
You can just add the variable to the href,
When you clink it while the address is
../index.php?id=5
trust me you then go to
../index.php?id=5¤t=1
BUT if you click that link again, than you 'll go to
../index.php?id=5¤t=1¤t=1
Actually I thinks that's tricky and bad practice to just append the variable.
I suggest you to do it like:
<?php
$query = isset($_GET) ? http_build_query($_GET) . '¤t=1' : 'current=1';
?>
A Label
take a look http://us.php.net/manual/en/function.http-build-query.php
I don't know why in Earth you would need this, but here we are. This should do the trick.
$appendString = "¤t=1";
$pageURL = $_SERVER["REQUEST_URI"].$appendString;
$_SERVER["REQUEST_URI"] should return just the name of the requested page, with any other GET variable attached. The other string should be clear enough!
i have a location menu that has to change location, the good thing is every url exist in every city,, and every city is a subdomain
city1.domain.com.uk/index.php?page=category/238/12
city2.domain.com.uk/index.php?page=category/238/12
Im trying this. Im trying to break the URL to remove subdomain , so i can replace it for each item in menu
I want to get index.php?page=category/238/12
<?PHP
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
// break it up using the "."
$urlb = explode('.',$url);
// get the domain
$dns = $urlb[count($urlb)-1];
// get the extension
$ext = $urlb[count($urlb)+0];
//put it back together
$fullDomain = $dns.'.'.$ext;
echo $fullDomain;
?>
But i Get this php?page=category/238/12
Also i havent think in a solution for an issue i will be facing with this..
If im looking at a product the url change to something like
city2.domain.com.uk/index.php?page=item/preview/25
But, the products dont exist in every city , so my user will get a 404.
=(
How can i make a conditional in the process so if page=item/preview/25 i do replace this for
page=index/index
You can split the domain as:
$url = "city1.domain.com.uk/index.php?page=category/238/12";
list($subDomain, $params) = explode('?', $url);
list($domain, $sub) = explode('/', $subDomain);
$newUrl = $sub . "?" . $params;
echo $newUrl;
Cheers!
How about this:
<?php
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$url = $protocol . '://' . $host . $script . '?' . $params;
$url=(parse_url($url));
$dns = substr($url['host'],stripos($url['host'],'.')+1);
$fullDomain =$url['scheme']."://".$dns.$url['path']."?".$url['query'].$url['fragment'];
if (substr($url['query'],stripos($url['query'],'=')+1,stripos($url['query'],'/')-stripos($url['query'],'=')-1)=='item') {
echo "redirect";
} else {
echo "don't redirect";
}
echo "<br>".$fullDomain;
?>