$SERVER variable and the double localhost - php

I've built a function that basically links an excerpt to its relative post. In order to do that I have figured that the variable $_SERVER['SERVER_NAME'] could come in handy in building the path.
I don't know if this is due to the fact I am testing the page on a local environment, but on my local machine the path I have is like http://localhost/webdir/localhost/index.php?p=3 localhost is repeated twice. What could possibly cause this?

Try even this
Edit works great for almost all type of URL..
<?php
$l1=explode('?',$_SERVER["SERVER_NAME"]);
$link='http://'.$l1[0];
echo $link;
echo '<h2><center>Add users</center></h2>';
$pageURL = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$pageURL1 = $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$pageURL=$pageURL.$pageURL1;
//$pageURLt=explode('?',$pageURL);//Uncomment this and next line if you don't want get variable
//$pageURL=$pageURL.$pageURLt[0];
echo $pageURL;
?>

use like this
$link='http://'.$SERVER_['SERVER_NAME'];
And if you don't want any GET type of value use this
$l1=explode('?',$SERVER_['SERVER_NAME']);
$link='http://'.$l1[0];
Hope this help you.

Related

BASE_URL in codeigniter shows different URL

I have received website code from somebody else & it's giving a lot of errors in loading. Every CSS, image or anything is loaded by this code
href="<?php echo asset_url();?>/css/...
Now I checked, asset_helper.php & found out this
function asset_url(){
return BASE_URL.'public'
}
In my config.php, line says $config['base_url'] = '';.
Finally when I tried to echo asset_url();, it gives me https://somerandomwebsite.com/.... I'm not sure from where this is coming from.
Sorry I'm new to CodeIgnitor & tried everything I could to find out but there was no luck. Can anybody help me in this?
Your Helper function might be using constant function. Check your constan.php file.
define(BASE_URL, "http://test.com");
First things first: the manual!
https://codeigniter.com/user_guide/helpers/url_helper.html?highlight=base_url#base_url
ok so they made a new function just to deal with an extra subfolder name in stead of just doing base_url(). 'public' or base_url('public') and have defined a BASE_URL somewhere which is not part of the core of CI. Track that BASE_URL down and kill it if possible. You can and should use base_url().
I use this for base_url (avoids hardcoded url):
$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
$config['base_url'] = $protocol . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
I usually autoload the thing in config/autoload.php:
$autoload['helper'] = array('url');

PHP find text in self

if (strpos(file_get_contents($_SERVER['SERVER_ADDR']),'<html>')) {
echo 'true';
}
I'm trying to check if the current page contains a string, and if so echo true. I've tried this however it doesn't work.
$_SERVER['SERVER_ADDR'] contains a completely different value than you seem to be expecting:
The IP address of the server under which the current script is executing.
To get a working link to the current page:
$currentUrl = $_SERVER['HTTPS'] ? 'https://' : 'http://';
$currentUrl .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
However, this becomes a giant mess, if you're requesting your own page from that page's code you'll get a clusterf*ck of endless recursive retrievals which will eventually blow up your server. I would 'recommend' not downloading the page in which you are doing this.

Make WordPress Available from any domain

I'm trying to create a plugin that will allow WordPress to be accessed from any domain, of course provided that the domain is pointed to it.
I have filter hooks for option_siteurl and option_home which is proving to be useful in almost all cases.
However, it doesn't appear to be working for images that are attached to a post nor for header images of themes. It looks like for these, it's taking the database value of options -> siteurl.
I've tried update_option, but that hasn't done the trick either.
I'm using the following code to get the host:
public function getGoodURL() {
$scheme = ($_SERVER["SERVER_PORT"] == 80 ? "http://" : "https://");
$host = $_SERVER["HTTP_HOST"];
return $scheme.$host;
}
Thanks!
Might want to try putting the site url configuration in the config file i.e.:
$domain = sprintf('%s://%s',
$_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https',
$_SERVER['SERVER_NAME']);
define('WP_SITEURL', $domain);
define('WP_HOME', $domain);
That way, your site will always accept the current domain.

Getting current domain + directory in php

So, I've found a way to get the current directory using dirname(__FILE__) and getting the domain with $_SERVER['HTTP_HOST']. While both of these are well and good, they aren't quite what I need them to be.
For instance, if I have a script on http://mydomain.com/scripts/myscript.php, I'd like to get http://mydomain.com/scripts/. I feel like there should be an easy way to do this and that I've somehow overlooked something.
As an aside, I am currently using the script in a cloud shared hosting environment, so the directory structure is somewhat odd.
Try:
<?php
echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
The only problem with that is that dirname returns the parent directory, so if you access http://domain.com/scripts/ directly you'll just get http://domain.com/ withouth the scripts. http://domain.com/scripts/script.php resolves correctly to http://domain.com/scripts/ though.
Try:
<?php
echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
function url_part(){
$http=isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$part=rtrim($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME']));
$domain=$_SERVER['SERVER_NAME'];
return "$http"."$domain"."$part";
}
echo url_part;//htts://www.example.net/test

How to retrieve the URL of a page using PHP?

Newbie question here, is there any inbuilt PHP tag that can be used to retrieve the URL of a page and echo it on the screen?
Thanks.
Echos the URL of the current page.
$pageURL = (#$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo $pageURL;
If your web server runs on standard ports (80 for HTTP or 443 for HTTPS) this would work:
getservbyport($_SERVER['SERVER_PORT'], 'tcp') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
Take a look at the $_Server variable. Specifically you probably want the REQUEST_URI value.
$base_url = _SERVER["HTTP_HOST"];
$url_path = _SERVER["REQUEST_URI"];
echo $base_url.$url_path;
Assuming the requested page was http://sample.org/test.php, you would get:
sample.org/test.php
You would have to add more $_SERVER variables to get the scheme (http://). REQUEST_URI also leaves any GET variables intact, so if the page request was http://sample.org/test.php?stuff=junk, you would get:
sample.org/test.php?stuff=junk
If you wanted that left off, use $_SERVER['PHP_SELF'] instead of REQUEST_URI.
If you want a really easy way to see which global variables are available, create a page with the following:
<?php
phpinfo();
?>
and put that script in any directory you are curious about. Not only will you see all sorts of neat info, you will also see how various factors such as HTTP vs HTTPS, mod_rewrite, and even Apache vs IIS can set some global variables differently or not at all.

Categories