haunted85
haunted85

Reputation: 1671

$SERVER variable and the double localhost

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?

Upvotes: 0

Views: 1374

Answers (2)

Rishabh Goyal
Rishabh Goyal

Reputation: 99

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.

Upvotes: 0

Rishabh Goyal
Rishabh Goyal

Reputation: 99

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;

?>

Upvotes: 1

Related Questions