srwright Posted May 30, 2014 Share Posted May 30, 2014 (edited) 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 May 30, 2014 by srwright Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/ Share on other sites More sharing options...
ginerjm Posted May 30, 2014 Share Posted May 30, 2014 Actually for your example, you don't need to include the ip. The link will work with just this: <link rel="stylesheet" href="/main.css" type="text/css"> Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/#findComment-1481402 Share on other sites More sharing options...
sKunKbad Posted May 30, 2014 Share Posted May 30, 2014 (edited) 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 May 30, 2014 by sKunKbad Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/#findComment-1481403 Share on other sites More sharing options...
srwright Posted May 30, 2014 Author Share Posted May 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/#findComment-1481406 Share on other sites More sharing options...
.josh Posted May 30, 2014 Share Posted May 30, 2014 There is a way to do that, yes. You can set the auto_prepend_file directive in your php.ini file. Note though that this will cause it to be executed on every request to a php script. Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/#findComment-1481408 Share on other sites More sharing options...
Jacques1 Posted May 30, 2014 Share Posted May 30, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288881-external-variable-for-url/#findComment-1481409 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.