Jump to content

Recommended Posts

Hi all,

 

So im trying to improve on my PHP as my knowledge isn't that great.

 

lets say for argument sake my ip is:

 

192.168.0.1

 

im trying to set a external variable to set this as the url.

 

so on each page i use, for each link instead of typing out the ip address i can simply type:

 

<a href="{url}/index.php">Home</a>

 

 

 

and then if it is possible, to do something like this:

 

<link rel="stylesheet" href="{url}/main.css" type="text/css">

 

all in one file so in my php pages i can simple include 1 php file and it will have all of the relevant stylesheets ect linked to it.

Edited by srwright
Link to comment
https://forums.phpfreaks.com/topic/288881-external-variable-for-url/
Share on other sites

ginerjm is correct, but for the sake of learning what you wanted to do, most of the time you will not be using an IP address, but rather a domain. If you really do need the IP address, you can use the server variable $_SERVER['SERVER_ADDR'].

 

Likewise, if you really did want to use the domain, you would use $_SERVER['HTTP_HOST'].

 

Example:

<?php

$domain = $_SERVER['HTTP_HOST'];

echo '<a href="http://' . $domain . '">Link</a>';

echo '<br />';

echo '<a href="http://' . $domain . '/contact">Link to contact page</a>';
Edited by sKunKbad

yes i know that it would be a domain normally. which is why i said for arguments sake...

 

and i also know just /main.css would work... but im trying to do what skunkbad said and simply learn how to do it.

 

that actull setup i have is completely different to this, i just made a quick example on here.

 

i want to have 1 global variable page called global.php with something like this to be used on each php page.

 

$url = $_SERVER['HTTP_HOST'];

 

would i have to use include to include the global file on each page. or is there a way to just make the variables accessible from all pages

 

sorry if this isnt making much sense.

This indeed makes no sense.

 

First of all: Never trust $_SERVER['HTTP_HOST']. This variable is defined by the client and can contain any garbage they want. If you carelessly insert the raw value into your HTML markup or queries, it can even be used for a code injection attack.

 

Secondly, you already have a variable with the host name: $_SERVER['HTTP_HOST']. Your $url variable is just useless. I'm not sure why PHP programmers have this strange idea that values are only “real” if you put them into a custom variable.

 

The proper solution is to actually define a variable or constant with the host name and then include this in every script. Script inclusion is a natural part of programming, and you will need this either way as soon as your application becomes non-trivial. Trying to avoid it is rather silly.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.