Variables and includes and routing, oh my!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Variables and includes and routing, oh my!

Post by vbmark »

Hello,

I have setup my site so that a user can go to a friendly folder name like this...

http://www.mydomain.com/cars

instead of

http://www.mydomain.com/page.php?location=cars

In the page where this is handled (we'll call this master.php) I do an include like this....

Code: Select all

include ("http://www.mydomain.com/page.php?location=cars");
It works great except for one thing, my variables are not working in page.php.

There are two ways I have tried to handle this:

1) Use the variable from the master.php in page.php. However, for some strange and frustrating reason I just cannot figure out, I can not get page.php to recognize variables set in master.php.

I've tried setting global all over the place that still nothing.

I thought it was perhaps because of the full URL in the include so I tried variations on...

Code: Select all

 
include ("page.php?location=cars");
and 
include ("../page.php?location=cars");
etc.
But I get 'page not found' errors. It seems it needs the full URL.

2) So I thought fine, I'll get my variables in page.php from the database again. They all work well except for one thing;
my session cookie that indicates if my users are logged in does not seem to work. On page.php (when included via master.php), my users always appear to be logged out. This is a mystery to me too. If I go straight to page.php my session cookie works fine.

Well, any insight would be appreciated.

Thanks!
mmoussa
Forum Newbie
Posts: 8
Joined: Wed Mar 18, 2009 8:43 am

Re: Variables and includes and routing, oh my!

Post by mmoussa »

1) Your variables are not working in page.php because it is being loaded remotely via the include call. So basically, your variables are not
within page.php's scope.

Note: You should avoid using urls in include calls. It presents a security issue and is not good practice.

2) That's because when page.php is included remotely (remember, you're using a url), the session is not being started by your user. The
session in this case is being started by the PHP server and it won't have access to this user's session variables.

I'm guessing your page.php looks something like this

Code: Select all

 
switch ($_GET['location'])
{
    case 'cars':
        do_stuff_for_cars_here();
        break;
    case 'something else':
        do_stuff_for_something_else_here();
}
 
Instead, include('page.php') in your master.php and call whatever function you need to call to display the appropriate page directly.
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: Variables and includes and routing, oh my!

Post by William »

vbmark: is there any reason you aren't using something like mod_rewrite for something like this?

Edit: If you're using Apache It would be a lot easier just to do something like....

Code: Select all

 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
RewriteRule ^(.*)$ page.php?location=$1 [L,QSA]
 
Then you would just need to parse the location variable in page.php to figure out where they're actually trying to go. For instance if you went to http://www.mydomain.com/cars/category/1 then the location variable would be cars/category/1.
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Re: Variables and includes and routing, oh my!

Post by vbmark »

mmoussa wrote:1) Your variables are not working in page.php because it is being loaded remotely via the include call. So basically, your variables are not within page.php's scope.
Ok, so it seems like what I want to do can't be done. That is, access the variables of master.php.
mmoussa wrote:Note: You should avoid using urls in include calls. It presents a security issue and is not good practice.
Even if it's the same domain?
mmoussa wrote:2) That's because when page.php is included remotely (remember, you're using a url), the session is not being started by your user. The session in this case is being started by the PHP server and it won't have access to this user's session variables.
Ah, didn't know that.
mmoussa wrote: I'm guessing your page.php looks something like this

Code: Select all

 
switch ($_GET['location'])
{
    case 'cars':
        do_stuff_for_cars_here();
        break;
    case 'something else':
        do_stuff_for_something_else_here();
}
 
Instead, include('page.php') in your master.php and call whatever function you need to call to display the appropriate page directly.
Actually, the $_GET['location'] would contain a user's name so I pull information dynamically from a database and build a page on the fly.
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Re: Variables and includes and routing, oh my!

Post by vbmark »

William wrote:vbmark: is there any reason you aren't using something like mod_rewrite for something like this?

Edit: If you're using Apache It would be a lot easier just to do something like....

Code: Select all

 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
RewriteRule ^(.*)$ page.php?location=$1 [L,QSA]
 
Then you would just need to parse the location variable in page.php to figure out where they're actually trying to go. For instance if you went to http://www.mydomain.com/cars/category/1 then the location variable would be cars/category/1.
Yes, that's what I'm doing. The problem isn't with getting to the destination I desire, but getting the variables either from the master.php to the page.php. Or, alternatively, getting the session cookie to work on page.php.

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . notfound.php
The notfound.php file processes the url, gets the username and pulls the appropriate data.
vbmark
Forum Newbie
Posts: 19
Joined: Sun Feb 15, 2009 8:53 pm

Re: Variables and includes and routing, oh my!

Post by vbmark »

mmoussa wrote:Instead, include('page.php') in your master.php and call whatever function you need to call to display the appropriate page directly.
What I ended up doing was just putting the code in the master.php and not doing an include. That worked fine.

Thank you for your help. You helped me understand the problem so I could find a workable solution.

Thanks!
Post Reply