Replace HTTP_HOST from current URL in php - php

Suppose my current url is:
www.example.com?id=10
if($_GET['id']==10):
//replace current $_SERVER[HTTP_HOST]; with www.example1.com without removing other parts of url and redirect.
else :
//replace current $_SERVER[HTTP_HOST]; with www.example2.com without removing other parts of url and redirect.
endif:
I need to replace current $_SERVER[HTTP_HOST].
How to do this using php.

If you want to redirect then here is the code-
$request = $_SERVER[REQUEST_URI];
if($_GET['id']==10):
$url = "www.example1.com/".$request;
else :
$url = "www.example1.com/".$request;
endif;
else I would like to know for what purpose you want to change current url.

Related

How do you insert part of page url inside the brackets of php function?

I'd like to insert part of the page url where the php function is displayed inside the brackets of the function. For example, my page's url is: www.mydomain.com/directory/**page**
I'd like to take /page and insert it inside: function(here)
Wont this do?
<?php
$url = "www.mydomain.com/directory/page";
$url = explode('/', trim($url, '/'));
print( $url[count($url)-1]);//prints "page"
//somefunc(page)
?>
Split the url by by / and select the last part of it. Then you can use it in any function.

Php get last part of url and remove /

Im using an angular script for my site.
I need dynamic titles so I decided to use php to fetch the last part of the url to display for the page title.
This is the code im using. This code is out putting my full url.
<?php
function curPageURL() {
$url = $_SERVER['REQUEST_URI'];
$url_array = explode('/',$link);
$lastPart = array_pop($url);
return $lastPart;
}
echo curPageURL();
?>
The problem is its fetching the whole url.
For example here is my url http://www.istreamradio.ie/category/Music
I need to display just the last part of the url in this case is Music.
Thanks In advance.
You can use end() function after exploding the URL . Please try end instead of array_pop.

remove site url from links Wordpress

I have to display link into my website like this :
$url = 'google.com';
Check it !
What I don't understand now is that, the output href is not google.com but mydomain.com/google.com.
I don't know how to resolve this issue.
You need to append http:// before your url
Try below code:
$url = 'http://www.google.com';
Check it !
Please use this code for site url .
$url = 'http://google.com';
Check it !
If you don't add http then it will open your domain name and then your $url variable name so please add http in your url.

URL output from database

With this code:
if(empty($aItemInfo['url'])) {
$url = '<p> </p>';
} else {
$url = ' | LINK';
}
I've got this as output:
http://localhost/tester/www.google.com
In db there is only www.google.com and ofcourse it's fictional.
What am I doing wrong?
You need to add http:// while parsing your code, before using it in the <a> tag.
If all your URLs will be without http:// use this code:
$url = 'http://'.$aItemInfo['url'];
Then use $url
Not too sure what you're trying to link to. If you're linking to an external site you'll need to add http:// in front of the link. If not, the link will be added to the end of the current domain name as shown above
You can put links are relative or absolute paths. If you don't include the "http://" part, then it assumes that it is a relative path. Add href="http://'.$aItemInfo['url'].'"

Remove string from the link href

I'm using a jQuery pagination script and I'm using the onChange function so if a user click on the page number it does redirect him to the $_SERVER['REQUEST_URI'] + it adds an page number to the request url, but if I will click on some pages several times then the request url looks like this: &page=3&page=1&page=10 ... etc.
The code looks like this:
onChange : function(page){
window.location = '" . $_SERVER['REQUEST_URI'] . "&page='+page;
}
Now I need to remove $page=??? from the url if it already exist.
After this
$url = $_SERVER['REQUEST_URI'];
$url = preg_replace_all("/\\&page=[^\\&]+/", "", $url);
$url will contain the url barring the page attribute
The reason for this is that every time the user clicks on your link, the value of $_SERVER['REQUEST_URI'] is the current URL and you are just appending an extra string to the end.
You need to set the get variable to the page you want then just change this variable when your function is called. Something like:
$_GET["page"] = page;

Categories