PHP - different URL link depending on the web page - php

Creating a website and in the header I want the URL a link to be different depending on which directory it is.
I want it to be:
- If directory1 then link to directory1 URL
- If directory2 then link to directory2 URL
Can someone help with it?
I found the code below which might be helpful.
Thanks
<?php
$homepage = "/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage==$currentpage) {
echo '/';
}
?>

Now i understood your question you wanted to get the main directory. If this is so than explode the url and than replace the http:// or https:// section from the array and than output the variable
<?php
$url="http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$url_orignal= explode('/', str_ireplace(array('http://', 'https://'), '', $url));
echo "www.website.com/".$url_orignal[1];
?>
Hope this helps you

Related

Using file_get_contents And Changing Domain of Links on the Included Page?

On my page I have this piece of code:
<?php
$homepage = file_get_contents('http://www.nbc.com/');
echo $homepage;
?>
It includes the NBC website perfectly, but I notice that all of the links on the NBC.com website start with my domain name instead of http://nbc.com so they don't work.
So for example instead of http://nbc.com/the-blacklist/episodes being displayed on http://nbc.com website, its displaying http://my-domain.com/the-blacklist/episodes.
Is there any way to use file_get_contents do include a page into my URL, but make sure all the links on the page are the original links so they work fine?
The links inside of pages you are pulling are using relative links (/page.html) instead of the full URL, you will need to do a Regex on the variable or string replace. To test it out you could do the following:
$domain = 'http://www.nbc.com';
$pull = file_get_contents($domain);
echo str_replace('href="/', 'href="' . $domain . '/', $pull);

How to get current page URL in Drupal?

I'm trying to output the current page URL on a Drupal site (Drupal 7), I've tried the following...
<?php print current_path(); ?>
Which outputs the page name but not the URL, any ideas on how to output the full URL too?
In straight PHP you can use this:
$curPathName = $_SERVER["DOCUMENT_ROOT"];
$currentURL = $_SERVER["HTTP_HOST"];
I don't see why that wouldn't work. That will give you the path and the URL.
You can use
<?php
global $base_url;
print $base_url . '/' . current_path();
?>
If you want to keep things done "the drupal way", you can use current_path() or request_path().
https://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7
You can also use $base_url instead of relying on $_SERVER.
The solution durbpoisn gave will give you the URL in the browser. If that is an alias, you can use drupal_get_normal_path() to get the internal path
https://api.drupal.org/api/drupal/includes!path.inc/function/drupal_get_normal_path/7
in drupal9
$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
On D7 if you want to get the current path you can use:
$path = drupal_get_path_alias();
So if your current URL is:
www.test.com/hello/world
You will get:
'hello/world'

Get current name for path in url

How can i grab everything that is after http://www.domain.com/somefolder/ in php.For e.g if http://www.domain.com/somefolder/login is accessed , i just need the login part.
I tried the following
basename($_SERVER['PHP_SELF']); and basename(__FILE__); both gives me index.php
You can do it using strlen and substr functions:
<?php
$url = 'http://www.domain.com/somefolder/login';
$remove = 'http://www.domain.com/somefolder/';
echo substr($url, strlen($remove));
EDIT And you should make sure that your urls always start with www. subdomain. Otherwise you can get unexpected results.
I found the answer
$_SERVER['REQUEST_URI'] was what i need
P.S Ty for the downvotes

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'].'"

Get Full URL with PHP & Include Subdomain

I have been using the following to discover the full url, but it does not detect the subdomain I am calling from.
$current_url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."";
echo $current_url;EXIT;
Any advice?
$_SERVER['HTTP_HOST'] should include the subdomain. Can you link us to an example page?
Tested and works fine for me. Used this code:
<?
$current_url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."";
echo $current_url;EXIT;
?>

Categories