Get current page url and replace root with a domain - php

I would like to change the following code in the following way.
The php script gets the current url of the page of mine.
Now since I am using the descriptions of what i have on my another site i would like to put canonical tag on the page.
So I would like the script to get the url of my site and replace the www.mysite.com to www.domain2.com
How do i make it that the ,," will result the url i want? ( so i can make " rel="canonical" />
It can be beneficial for others aswell, who own more sites and have the same description and dont want to get penalized by Google Panda or have product descriptions from manufacturers.
<?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;
}
?>

I have finally found a solution to that
<?php
$Get_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo str_replace("www.mydomain.com", "www.domain2.com", $Get_url);
?>

Related

How to get specific part of url?

I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
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;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php

Get the URL users came from on an Error 404 Page using PHP?

I'm currently building an Error 404 page for a website project. People are redirected to this page using .htaccess if the page they requested can't be found.
I want to display on the custom Error page the URL or link that they came from, but I can't figure out how. I had a go at this:
<?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; // Finally, Returns the page URL
}
?>
And then I added the PHP function to my Error page:
The Page You Came From Was: <?php echo curPageURL(); ?>
Sadly, all this does in the end is display the URL of my Error page, and not the one where users were just redirected from.
Anyone have any ideas?
Adam.
You should use
$_SERVER['HTTP_REFERER']
But it wont always be set.

Getting url of the current page

I am trying to get the website url of the current page on which the person is. I would use it in the social share buttons that I am creating. The websites are all Wordpress websites. I got the following url for getting the current url,
<?php if (is_home()) { echo site_url(); } else { echo the_permalink(); } ?>
The script does not execute and when I click the button, the url does not generate but the php script is displayed in the browser as it is.
Please help out. Thanks
You would use
<?php echo $_SERVER['PHP_SELF']; ?>
This gives you the url of the current page. For example index.php or something similar
Using Javascript:
var currentURL = document.URL;
alert(currentURL);
Using PHP:
<?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;
}
?>
If you want file name
Use PHP Magic Constants such as __FILE__

Is the current page the base url in PHP?

I need to see if the current page a user is on is the main page of the website, i.e. there is nothing after the base url.
I'm doing this to exclude some code off the main page.
I asked this question is Javascript, but would like to implement it in PHP
This will probably give you what you are looking for:
$is_home = $_SERVER[ 'REQUEST_URI' ] === '/' ? true : false;
<?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;
}
?>
This should give you the url of the current page, when you have this you can check it against the home url.
The following should do the trick:
if (empty($_SERVER['QUERY_STRING'])) {
// no params
}

checking user's current page and condition statement

guys im looking for a comprehensive code to check if a visitor is in index.php then showing some lines to him
i mean i want to consider every possibility for this 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;
}
$curentUrl = curPageURL();
if (preg_match("/index.php/",$curentUrl) || preg_match("/index.html/",$curentUrl)) {
}
but im looking for a more complete code to see if user is in index page of my website
Actually this curPageURL() function has nothing to do here
if ($_SERVER['PHP_SELF'] == "/index.php")
is enough.
But on most PHP sites index.php doesn't mean only index page. but it can be whole site too.
Without complete circumstances this question is too broad to answer.
What about
$_SERVER['SCRIPT_NAME']

Categories