Jump to content

Using Variables etc , confused a bit


A2enMOD

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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 by PFMaBiSmAd
Link to comment
Share on other sites

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.