1

Ok... I have a ton of different pages all which have the same little block of code. Up to now, I have been updating the $max = 150; over all the pages manually as the number changes. So I tried making a max.php with this single line in it and referencing it from all the others.

However.. here's a problem I have...

/vars/max.php

$max = 150;

/inc/changes.php

include 'vars/max.php';
echo $max;

Returns 150

/update/updatepage.php

include 'vars/max.php';
echo $max; 

Returns 0

So.... It works on one page, but not on another. If I change max.php to this...

/vars/max.php

<?php
    $max = 150;
?>

then the /inc/changes.php returns 0, and /update/updatepage.php returns 150.

Any ideas why?

I have already tried changing it to $GLOBALS["max"] but it's the same.

Is there a better way to do this so I only have to update the value in one place? I don't want to use a mysql database as my hosting company is a bit iffy with how many connections are in use.

Thanks for any help you can give.

Community
  • 1
  • 1
Cully
  • 484
  • 9
  • 19
  • 1
    Are you expecting the changes you make to `$max` to persist between requests? – Gian May 07 '13 at 07:17
  • 2
    You are saving `$max = 150;` but you are including `rises.php` so do your `rises.php` file have `max.php` included? – Mr. Alien May 07 '13 at 07:17
  • 1
    what is the relation between `max.php` and `rises.php`? – Amir May 07 '13 at 07:22
  • use DEFINE, for constants – qisho May 07 '13 at 07:32
  • @Mr.Alien Sorry... That was a typo. I have updated to show that they are both referencing `include 'vars/max.php';` – Cully May 07 '13 at 07:45
  • @Amir Sorry... That's a typo.. rises.php should actually be max.php in my code.. I've updated it now. – Cully May 07 '13 at 07:46
  • 1
    @Cully Path are different too – Mr. Alien May 07 '13 at 07:46
  • The first version of `max.php`, without `` tags, **should not work at all.** – deceze May 07 '13 at 07:46
  • @deceze I agree... but it does. I'm just trying to get it constant between all my files. I thought someone might have come across this before... or maybe I'm making this overly complicated. – Cully May 07 '13 at 07:48
  • Directories are different, use complete paths or provide correct relatives path. This should already work then – Hanky Panky May 07 '13 at 07:48
  • Let's put it this way: **it does not work without `` tags!** Where you *think* it works it probably does for different reasons. It's only correct *with* `` tags. – deceze May 07 '13 at 07:49
  • @deceze Ok, but it's getting the 150 from the max.php file without the `` tags being there. I don't understand it and that's why I'm asking on here. I know it's getting it from that file because when I change the number on ONLY that file, it picks it up on my `/inc/changes.php`. – Cully May 07 '13 at 07:55
  • @ØHankyPankyØ If I put `include 'http://www.website.com/vars/max.php';` then it does the exact same thing. Is that what you meant? – Cully May 07 '13 at 07:56
  • @Mr.Alien If I put `include 'http://www.website.com/vars/max.php';` then it does the exact same thing. Is that what you meant? – Cully May 07 '13 at 07:57
  • 1
    @Cully Path is making troubles for you here and for the absolute path, read this http://stackoverflow.com/questions/4369/how-to-include-php-files-that-require-an-absolute-path – Mr. Alien May 07 '13 at 07:59
  • @Mr.Alien Thank you so much for that link.. it solved my problem. I just put `include $_SERVER["DOCUMENT_ROOT"].'/vars/max.php';` and it works on all of my files. – Cully May 07 '13 at 08:15

4 Answers4

1

Probably the file path over here is resulting to this

Try using absolute paths for your includes, answer over here will help you

Declared Variable Path - /vars/max.php

Including In Files Path

  • /inc/changes.php
  • /update/updatepage.php
Community
  • 1
  • 1
Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
  • Thank you... I tried changing to `include 'http://www.website.com/vars/max.php';` and it didn't work, but then as per your link, I changed it to `include $_SERVER["DOCUMENT_ROOT"].'/vars/rises.php';` and it works perfectly! – Cully May 07 '13 at 08:24
  • 1
    Also I'd like to add that **max.php** uses the `` tags with this. I'm not sure how it 'worked' without. – Cully May 07 '13 at 08:26
0

Try to include like this

include 'vars/rises.php';
echo $max;

in the pages that you want to access the $max variable...you are only including the 'rises.php' but your max value is given at the 'rises.php'

Gautam3164
  • 28,027
  • 10
  • 58
  • 83
0

Not sure why you're getting 0, but I have run into problems before when multiple files try to include the same file in one request. Try using include_once "vars/max.php" instead.

That way it only gets added to the include path once, and you shouldn't have any more conflicts.

Scott Terry
  • 1,213
  • 1
  • 14
  • 20
  • I've just changed them all to `include_once 'vars/max.php';` but there is no change. – Cully May 07 '13 at 08:00
  • 1
    in that case it seems like the problem may be the path you're using. In your post, you specify "/vars/max.php" as the path to the file. Try writing that into your code exactly. Instead of "vars/max.php" (without a leading forwardslash), use "/vars/max.php" (with a leading forwardslash) – Scott Terry May 07 '13 at 08:05
  • I tried with both a forwardslash and without... same result on both files. Putting `include 'http://www.site.com/vars/max.php';` also didn't work, BUT it's now working when I put in `include $_SERVER["DOCUMENT_ROOT"].'/vars/max.php';` – Cully May 07 '13 at 08:12
  • 1
    okay. it's important to note something about file paths here. if you put a "/" in front of your path, you're comuncating that the folder/file is at the root of your file system. $_SERVER["DOCUMENT_ROOT"] provides the actual root to the file system, which is why it's working. In php, the include function looks for files using an acutal file path. The Http path won't work. – Scott Terry May 07 '13 at 08:16
  • Thanks - yeah I'm quite a beginner with this. I didn't realise you should use the root url given in `$_SERVER["DOCUMENT_ROOT"] ` and I assumed that http paths would work. I need to hit the books. – Cully May 07 '13 at 08:29
0

Problem could be the updation of variable through script. You probably setting $max=0; otherwise it has to be null.

My point is, If it a constant deal it as a constant, use php define function. Otherwise your scripts will be modifying it and as it is global you will be stucked to figure out where it has been modified.

Waqar Alamgir
  • 9,360
  • 4
  • 28
  • 36
  • it's not a constant. It will be changing every few days. 6 or 7 different pages use this variable and I have been manually updating each page to date. I want to be able to update one place which updates all of these pages... – Cully May 07 '13 at 08:14
  • so what use, define('MY_MAX' , $someValueFetchedFromSomewhere); – Waqar Alamgir May 07 '13 at 08:16