Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Menu fails on some .htaccess-rewritten URLs.
Old 04-13-2006, 03:05 PM PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
I am using PHP (plus some javascript) to display the correct submenu, with the correct link marked as 'active', for each section of my site. The PHP was originally put together by the very helpful Oberon here on the forum, in this thread.

It has been working fine for me since then, but now I have run into some problems as I am trying to move the whole site into the CMS I use (ExpressionEngine). Previously, I kept some pages static.

Previously, the PHP was working fine both on the EE-driven (example: http://www.westeros.org/Citadel/SSM/) and the static (example: http://www.westeros.org/Citadel/Concordance/) sections of my site. However, as I tried to move http://www.westeros.org/Citadel/ (=http://www.westeros.org/Citadel/index.html) to EE as well, the menu failed to work and I got the error 'Undefined index: //' for these lines:

Code:
$MainMenuItem = $WhichMainMenuItem[$CurrentSection];

$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];

$SubMenu = $WhichSubMenu[$CurrentSection];

$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
The whole of the PHP script and the relevant parts of the .htaccess can be viewed here:

http://www.westeros.org/Citadel/Code.txt

The errors can be seen 'live' at http://www.westeros.org/Citadel/index.php/, as I have temporarily restored the index.html for access through http://www.westeros.org/Citadel/.

Of the rewrites in the .htaccess, those involving 'Index' are those that may be having an effect, since that's the default template for this URL that would be called at http://www.westeros.org/Citadel/ if I didn't have an index.html.
Linda is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-13-2006, 04:43 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Oh, okay, I think I have an idea.

When http://www.westeros.org/Citadel/ runs through the index.php, I imagine the _internal_ URL is http://www.westeros.org/Citadel/index.php/.

So, since part 2 of the exploded URL is index.php, the PHP will run through this part:

Code:
$Parts = explode("/",$URL);

if ($Parts[2] == "index.php")
{
  $Parts[2] = $Parts[3];
}

$CurrentSection = "/".$Parts[2]."/";
However, in this case, part 3 is nothing it results in $CurrentSection being equal to //.

After much further staring at the code, I think I've got it. I need to change this:

Code:
if ($Parts[2] == "index.php")
to something like this:

Code:
if ($Parts[2] == "index.php" AND ($Parts[3] !="" OR $Parts[3] != "Index"))
Basically, if the third part is either nothing or 'Index', then it shouldn't set part two to be equal to part 3. However, I am pretty sure the code above is incorrect for that kind of statement.

Last edited by Linda; 04-13-2006 at 07:00 PM..
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-14-2006, 03:35 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Some further testing:

1) http://www.westeros.org/Citadel/index.html

No errors.

echo $CurrentSection; yields /index.html/

2) http://westeros.org/Citadel/index.html

No errors.

echo $CurrentSection; yields /index.html/

3) http://www.westeros.org/Citadel/index.php

These errors:

Code:
Notice: Undefined offset: 3 in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 53
//
Notice: Undefined index: // in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 60

Notice: Undefined index: // in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 62

Notice: Undefined index: // in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 64

Notice: Undefined index: // in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 66
echo $CurrentSection; yields nothing at all (the initial error regarding there being no part 3 must kill it)

4) http://westeros.org/Citadel/index.php

This is the weird one, as there are no errors here.

echo $CurrentSection; yields //

So, after that round of testing, I am pretty much even more confused.

Last edited by Linda; 04-14-2006 at 04:23 PM..
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-15-2006, 07:08 AM Re: PHP Menu fails on some .htaccess-rewritten URLs.
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Wow... I just read the old thread about the original problem you had, it got kinda complicated... I'll take a look at this first chance I get (I'm at home over easter)
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-15-2006, 07:32 AM Re: PHP Menu fails on some .htaccess-rewritten URLs.
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
remove the "http://" before exploding the string $URL = str_replace('http://','',$URL)

then $parts[0] will always be the hostname (with or without the www) and $parts[count($parts)] will always be the filename.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-15-2006, 01:28 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Oberon,

Thanks. I should have known better than to try to add stuff this close to a holiday:

chris,

The problem sort of starts after the hostname. In the examples above, all URLs will start with http://www.westeros.org/Citadel/. Well, technically I guess they could be just www.westeros.org/Citadel/, or even westeros.org/Citadel/, but apart from in the oddity of example 4 in my third post, that has never made a difference.

The issue is what happens after the /Citadel/ part.

Lets take some examples:

1) Static page right under the /Citadel/ directory.

URL: http://www.westeros.org/Citadel/index.html. Internally it is the same.

In this case, parts[2] is not index.php but index.html, and it displays the default menu without generating any errors.

2) Dynamic page right under the /Citadel/ directory.

URL: http://www.westeros.org/Citadel/About/. Internally it is http://www.westeros.org/Citadel/index.php/Index/About/.

In this case, parts[2] is index.php, but there's no parts[3] or parts[3] is Index (which isn't part of the array). This generates errors.

3) Static page one directory deeper, for example /Citadel/FAQ/.

URL: http://www.westeros.org/Citadel/FAQ/. Internally it is the same.

In this case, parts[2] is FAQ, and it displays the FAQ menu without generating any errors.

4) Dynamic page one 'directory' deeper, for example /Citadel/SSM/.

URL: http://www.westeros.org/Citadel/SSM/. Internally it is http://www.westeros.org/Citadel/index.php/SSM/.

In this case, parts[2] is SSM, and it displays the SSM menu without generating any errors.

What really stumps me is why it works when $CurrentSection is set to /index.html/, but not when its set to // or /index.php/ or /Index/.
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-19-2006, 07:55 AM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
With the holidays over, I hope its okay if I give this one a bump to see if anyone might have any ideas.
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-19-2006, 03:35 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I'm trying to remember back to when you first set this up...

I'm not very familiar with rewrite rules. When you say 'internally' do you mean that the user sees for example http://www.westeros.org/Citadel/About/ while the PHP script gets http://www.westeros.org/Citadel/index.php/Index/About/ from $_SERVER[] ?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-19-2006, 04:36 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Right. You access the page via http://www.westeros.org/Citadel/About/, but as far as the server is concerned, its at http://www.westeros.org/Citadel/index.php/Index/About/.

The rewrites I am using are these:

First, allowing the URLs to be accessed without specifying index.php:

RewriteCond %{REQUEST_URI} ^/Citadel/Index
RewriteRule ^([^/]+)/([^/]+.*)$ /$1/index.php/$2 [L]

Second, if necessary (wouldn't be necessary if you were just going to http://www.westeros.org/Citadel/index.php), allowing the URLs to be accessed without specifying Index:

RewriteCond %{REQUEST_URI} ^/Citadel/About
RewriteRule ^([^/]+)/([^/]+.*)$ /$1/Index/$2 [L]

However, after staring at this all weekend, I am starting to think the rewrites may not be the issue as such.

The key to the problem is, I think, figuring out why it is that the array doesn't break down when the value of $CurrentSection is /index.html/ and why it does break down when its /index.php/, /Index/ or just //.
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-21-2006, 08:00 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Hmm, I think I might have another idea for how to test things.

Could this

Code:
$URL = $_SERVER['PHP_SELF'];

$Parts = explode("/",$URL);

if ($Parts[2] == "index.php")
{
  $Parts[2] = $Parts[3];
}

$CurrentSection = "/".$Parts[2]."/"; 

$MainMenuItem = $WhichMainMenuItem[$CurrentSection];

$MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];

$SubMenu = $WhichSubMenu[$CurrentSection];

$SubMenuItem = $WhichSubMenuItem[$CurrentSection];
be changed so that it will output on the page all the results? As in, listing each part it breaks the URL into (and which number each part gets), listing what $CurrentSection ends up as and listing what the last four variables end up as. Then I could test this file at different URLs, to see what the differences are.
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 04:49 AM Re: PHP Menu fails on some .htaccess-rewritten URLs.
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
certainly, for the single variables just echo out something like:
PHP Code:
echo "Currentsection = $CurrentSection"
For the arrays I usually do this:
PHP Code:
echo "<pre>";
print_r($Parts);
echo 
"</pre>"
That will print the whole array in a nicely formatted list so you can see what the parts are.

I'm still a bit lost as to the exact layout of the folders, and I can't work out what the re-writes are doing.
The other remaining option of course is to fake it - since it is complaining of a missing index '//', then just add an entry to your menu arrays mapping '//' to the right page.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-22-2006, 02:03 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Quote:
I'm still a bit lost as to the exact layout of the folders, and I can't work out what the re-writes are doing.
The other remaining option of course is to fake it - since it is complaining of a missing index '//', then just add an entry to your menu arrays mapping '//' to the right page.
The problem is that when it hits //, it should just be showing the default menu, and I am not sure how to add something to the array that results in the default, just like index.html results in the default.

I guess I still don't understand what the 'undefined index' error means, actually.

I'll give the echoing a try, though, to see if that sheds any light on what happens.
Linda is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 02:20 PM Re: PHP Menu fails on some .htaccess-rewritten URLs.
Experienced Talker

Posts: 43
Trades: 0
Here's an idea:

Code:
$URL = $_SERVER['PHP_SELF'];

$Parts = explode("/",$URL);

if ($Parts[2] == "index.php") 
Is it possible to add a check here 
to make sure that if $Parts[2] is index.php and 
either of the following is true, $Parts[3] doesn't 
exist OR $Parts[3] is not within the array, then 
terminate the script or set $CurrentSection to 
no value at all?
{
  $Parts[2] = $Parts[3];
}

$CurrentSection = "/".$Parts[2]."/";
Edited to add:

After staring at some examples of php code, I've cobbled together the following:

Stage 1: If part 2 is NOT index.php or index.html, go ahead and define current section. Not sure how I would get it to not to go onto stage 2 and 3 if stage 1 is as it should be, though.

if ($Parts[2] != "index.php|index.html")
{
$CurrentSection = "/".$Parts[2]."/";
{

Stage 2: If stage 1 wasn't true, check to see if part 2 IS index.html. If so, terminate (the default menu should be displayed, so no values need to be set). Again, if this is true, it shouldn't go on to stage 3.

if ($Parts[2] == "index.html")
{
die;
{

Stage 3: If part 2 is index.php and part 3 exists and part 3 is within the array, set current section. Otherwise, terminate.

if ($Parts[2] == "index.php") && ($Parts[3] != "NULL") && (array_key_exists($WhichMainMenuItem,$Parts[3]))
{
$CurrentSection = "/".$Parts[3]."/";
}
else
{
die;
}

Currently, this does not seem to work, but is it anywhere in the vicinity of being on the correct track?

Last edited by Linda; 04-22-2006 at 06:51 PM..
Linda is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Menu fails on some .htaccess-rewritten URLs.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47322 seconds with 12 queries