A2enMOD Posted January 5, 2013 Share Posted January 5, 2013 Hi, This little man in the back of my head keeps telling me that I am not using PHP efficiently enough when trying to tell code where the location of things are. I will give you a few examples: 1. For the start I created a variable to deal with the URL of the site like below: $base_url = "http://www.mysite.com/"; unitl recently I have learned a different way which I will be implementing using the define CONSTANT method like below: define('BASE', 'www.mysite.com'); The reason I want to use that is because I am putting together a function that will detect my server name like the example below: $host = $_SERVER['SERVER_NAME']; if($host == 'domain.com) { define('BASE', '$host'); } else { define('BASE', '$host'); } Firstly is the syntax correct above? 2. Images - when I check out the source of my site the location of the images are displayed like so: <img src="http://sitenorhost/images/home/image.png" title="random image" alt="random image" class="img"> but on other sites I have visited the display output is like this: <img src="/img/graphics/navigation/noTab/ns-logo.png" alt="Network Solutions" border="0"> Why is this, what have I done wrong. Of course from a PHP point of view I want a robust dynamic solution to locate the necessary content. This also goes for links like below: This is how I want it: <li><a href="/main/mypage.php">Mypage</a></li> but my ouput is like this: <li><a href="http://www.mysite.com/main/mypage.php">Mypage</a></li> this is because am using the variable $base_url . I believe it is not good to use the methods above and would like some sound detailed advice on how to make this work for me. I am a leaner at PHP and I have got to this stage so far. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/ Share on other sites More sharing options...
requinix Posted January 5, 2013 Share Posted January 5, 2013 1. It's alright, but you realize you're using the exact same define() in both branches? In any case there's a smarter solution if you want the hostname: define('BASE', $_SERVER['HTTP_HOST']); Then BASE will always be the hostname the person is visiting. 2. That's fine. People tend not to do that because it involves knowing the hostname, and inserting it into every link, but if you are more comfortable doing that then it's perfectly fine. ...Until you discover SSL. Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403329 Share on other sites More sharing options...
A2enMOD Posted January 5, 2013 Author Share Posted January 5, 2013 Ok that's great let me give that a try and report back Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403330 Share on other sites More sharing options...
bashy Posted January 5, 2013 Share Posted January 5, 2013 Also you only want to define it if it isn't defined. if (!defined('BASE')) define('BASE', $siteurl); Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403344 Share on other sites More sharing options...
Love2c0de Posted January 5, 2013 Share Posted January 5, 2013 Why don't you scan the site directory, locate through your folders to find your folder full of images, then obviously depending on your logic or what you want to do with said images, it depends on the code. Give this a try: $full_dir = dirname(__FILE__); $site_dir = scandir($full_dir); Of course it all depends where your images are located in relation to the script you are running this PHP in. Kind regards, L2c. Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403347 Share on other sites More sharing options...
Pikachu2000 Posted January 5, 2013 Share Posted January 5, 2013 In regard to the question about the href= attribute in your <img and <a tags, the correct way is to provide the full URL. IOW, the way you have it is already the right way. Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403357 Share on other sites More sharing options...
PFMaBiSmAd Posted January 5, 2013 Share Posted January 5, 2013 (edited) @bashy, I'm not following you around trying to pick on you, only on what you are suggesting. This is the 1st of two threads where you have suggested conditionally doing something that should be unconditional. Since you should only be trying to define any constant once, you should know if and where you have defined it, what value it has and what it means, and you would want an error if you tried to define it again. Conditionally defining it could result in unexplained operation of the page, since its first definition, perhaps in a main configuration file, would be the value that is used and not the value you just conditionally tried, but failed to set it to. Edited January 5, 2013 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/272715-using-variables-etc-confused-a-bit/#findComment-1403375 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.