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;
?>
Related
I can navigate to one view in two ways:
http://test.new.loc/ru/cabinet/intranet/update/
and http://test.new.loc/ru/plaint
I used $this->redirect(array('/plaint'));in actionUpdate action (for this url http://test.new.loc/ru/cabinet/intranet/update/ ) to redirect to this http://test.new.loc/ru/plaint page .
I used following code var_dump(Yii::app()->controller->action->id);, but it returns /plaint. But, I need to get /intranet/update/ url. How can I get it?
Check it out...
<?php echo $_SERVER['REQUEST_URI']; ?>
Thanks
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);
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'
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
i have developed a php application which is running perfect on local server. when i deployed the application on web server the links are not working
1) my site is "abc.myapplication.com" (abc is subdomain)
i defined following variable in config file
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
ROOT_PATH variable shows /home/punjabfo/public_html/abc (which is perfect)
for link i used following code
Add Record
link should go to "abc.myapplication.com/addrecord.php" but link go to
"abc.myapplication.com/home/punjabfo/public_html/abcaddrecord.php"
i tried a lot but could not fin the issue. please help. thanks
What is wrong with Keeping It Simple
Add Record
Let the server do all the work, as it gets it right and you do less messing around.
Try
define('ROOT_PATH', $_SERVER['HTTP_HOST']);
Just use
Add Record
You can try -
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.parse_url($url, PHP_URL_HOST) . '/';
Your issue is "$_SERVER['DOCUMENT_ROOT']". $_SERVER['DOCUMENT_ROOT'] stands for the root directory on the server (dir-path). What you need, is the URL and not the file-system-path.
Take a look on
<?php
echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
?>
Why not to do
Add Record
Of course you do not need ROOT_PATH in the URL. What you do is returning full path of the file, instead of link. And btw, full path is incorrect itself, as you forgot slash before addrecord.php.