Closed Thread Icon

Topic awaiting preservation: Need help with PHP for URL redirects, please! (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12634" title="Pages that link to Topic awaiting preservation: Need help with PHP for URL redirects, please! (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Need help with PHP for URL redirects, please! <span class="small">(Page 1 of 1)</span>\

 
Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-04-2003 04:31

Sorry for the long post, but I'm getting desperate here. Everything I do seems to either a) not work or b) bring down the server. If it was just a case of a), I would just go the trial and error route that has helped me solve any problems I've run into so far. Considering that my errors are bringing down the server that my site is on (along with every other account at my host that is on that server), trial and error is not a viable option. I no longer claim to have any clue as to what is going on here. I am baffled and indeed beginning to get a bit frantic.

OK, deep breaths... let me just relax here and back up a bit.

It all started with my forays into PHP for a solution to the "one HTML page for every single image in my gallery" problem. I was delighted to find that it was very easy to make a single PHP page that would eliminate the need for separate HTML pages. Seeing how easy it was to use, I decided to apply the same method to my archives and my writing section.

After doing this, I realized that liminality.org/photo.php?set=whitesands&pic=3 was a rather unweildy URL. On top of that, it depended on a specific technology, and if at any point in the future I decided to change technologies all links to that "page" would die. This wasn't so much of a problem when I was doing my image galleries, but I realized it would be quite a problem with my archives.

I did some hunting around and discovered an article on ALA dealing with URL redirects using PHP. I applied what I learned there to my three subdirectories (imagery, writings, archives), and everything worked splendidly. Now I could type liminality.org/imagery/whitesands/3/ to get the same thing as above. More importantly, it didn't matter what I did with my technology later on, as I could always change what was included in the PHP handler file.

Then I realized that it would be nifty to apply this to all my links, which would require an .htaccess file in my root directory that applied to the whole site, not just a single subdirectory (I was using three different .htaccess files in each of those subdirectories, with a separate handler file for each). Being the foolish and naive person that I am, I figured all the hard work was behind me and it would only be a matter of combining what I had done before. That was before I lost half of my hair and the remaining half turned gray.

My first obstacle was just general stupidity on my part. I had taken out the first part of the PHP handler file shown in the ALA article above (the part that checks to see if the URL is for a file that actually exists) because I wanted people to type liminality.org/writings/ rather than liminality.org/writings.html. This worked swimmingly. However, I made a mistake when I wrote up my handler file for the root directory: I had blank $URI_REQUESTs (in other words, requests for the root domain) try to include liminality.org/. As you can imagine, this created an endless loop as the file constantly went back to the server asking for the same file. Well, not endless--Apache just died after being flooded with hits. Twice, in fact, before I realized that I was the idiot bringing down the server.

The timing was just too perfect--the server would go down just as I tried to access the domain. I couldn't imagine that I could be doing anything to bring down the server, but I have learned that when something goes wrong that shouldn't, it's usually because I did something stupid. I opened up my handler file again and stared at the code for a few moments before it hit me.

"Oh my God..." was my first thought, followed quickly by intense mortification. I quickly sent off an e-mail to my host explaining what had gone wrong, but with all the e-mails he received about the server outages (he said "thousands" in the host blog :eek I doubt he has gotten to mine yet (and he may never). I quickly fixed the file to reference the actual html file instead of the domain and put the code that checks for an existing file back in. Problem solved.

Or not. I now had no problems getting to the actual domain, but when I tried to type in a subdirectory (let's say, for example, "writings") I got this error:

Fatal error: input in flex scanner failed in (my root directory)/writings in line 1

But I have my handler file set up to redirect any requests for /writings/ to /writings.html, which should work now because I have the check-for-existing-file code back in, right? I've tried to do some research on what the heck that error means (btw, is there anywhere in the PHP documentation that just lists all the error messages and what they mean?), and apparently it has something to do with the fact that you can't include directories (only files). I wasn't aware that I was trying to include a directory, though--I'm pretty sure that writings.html is a file.

I had no problems in my subdirectories, but I've thought about that and I realized that, although the URL may be asking for a subdirectory (where the .htaccess file is located), the actual handler file is located in my root directory and thus not subject to the .htaccess file in the subdirectory. I'm not sure what this means, but I'm pretty sure it is why I'm having problems. I just have no idea what I need to do to fix it.

As I said before, I would gladly continue in trial and error until I figured out the problem, but I just tried to test it out again--no error this time, just a dead server again. Fortunately, the host guy has written a script that automatically kills the server and brings it back up, so downtime is only about five minutes, but I'd rather not do anything to bring the server down at all (he's going to kill me if and when he ever gets to my e-mail). That's why I'm here. I don't know if this explanation made any sense, but that's probably because I have no idea what I'm doing, and all you PHP experts here are probably laughing at me and saying, "Ha, that's easy!" At least that's what I'm hoping.

OK, so here's the code for PHP handler file (at least the part relevant to the discussion here):

code:
<?php

//check to see if a "real" file exists

if(file_exists($DOCUMENT_ROOT.$REQUEST_URI)
and ($SCRIPT_FILENAME!=$DOCUMENT_ROOT.$REQUEST_URI)
and ($REQUEST_URI!="/")){
$url=$REQUEST_URI;
include($DOCUMENT_ROOT.$url);
exit();
}

//break the URI down
$url=strip_tags($REQUEST_URI);
$url_array=explode("/",$url);

$url1 = $url_array[1];
$url2 = $url_array[2];
$url3 = $url_array[3];

//redirects to the root directory page
if ($url1=="")
{
include("http://liminality.org/index.html");
exit();
}

//I originally had a switch in here, but I took it out to test just the writings part...
if ($url2=="")
{
if ($url1=="writings")
{
include("http://liminality.org/writings.html");
exit();
}
}

.
.
.

?>



Of course, it is possible that the server went down for another reason and I didn't do it this time. I just don't feel like taking that chance (seeing that everything else has been my fault). I'm really desperate here, and I would appreciate any help I can get. If any wants to ICQ me (my number is in my profile), I'm usually invisible, so just drop me a line.

[Edit: I didn't realize that UBB parsed URLs automatically... took the links out, as they don't lead to anywhere but test pages or errors]

[This message has been edited by Suho1004 (edited 03-04-2003).]

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-04-2003 09:23

OK, I've sat and thought about this for a while (since I'm too afraid to actually do anything at the moment), and I think I know what's wrong. I knew that it couldn't be the include in the second part (after the URL breakdown) because a) I'm not trying to call a directory and b) I tried replacing that include with an echo, just for testing purposes, and I got the same results. So, going back to maxim of "If something went wrong, I probably did something stupid," I took another look at the code. And then I realized what the problem is.

The problem is that I do have a directory entitled "writings"--it contains the content HTML files that get stuffed into the PHP template. Since file_exists checks for directories as well as files, that if statement was being evaluated as true, and the script was trying to include the directory. That's what the flex scanner crap was all about (and while we're on the subject, how's that for a helpful error message?). I could include a number of checks in the script to make it sure that such queries don't end up trying to include the actual directories for writings, imagery and archives, but I have a feeling that the easiest solution would be to just rename the directories so the file_exists check fails.

For those who may be following along at home, this is how I learn new stuff. Just what you always wanted, right--a peak inside my head. Up until now I have kept all the PHP crap to myself, but the server outages were just freaking me out (although, in retrospect, I don't think this particular problem had anything to do with the server outages), and I decided it would be safer to post here than to experiment. It turns out, though, that all I needed to do was use this silly little thing sitting on top of my shoulders. No, not my neck, the thing attached to my neck. Wiseacres...

Thank you, ladies and gentlemen. My name is Suho1004, and I am an idiot.

(Just my take on a line from "The American President." At least I think that was the English title--it was translated into Korean as "The President's Girlfriend." Those Koreans and their wacky women's lib ideas...)

[Edit: Yup, that was it. I just went and renamed those three subdirectories, and everything is fine. I tell you, the depths of my stupidity just astounds me sometimes. Now I have to go in and change all those img tags that reference the old subdirectory...]

[Edit2: Sigh, well, it worked for the first level (e.g. liminality.org/writings/), now I'm just trying to get it to work for the second and third levels (e.g. liminality.org/writings/backstory/1/). I think I've crashed the server again, which would mean that I've got another loop somewhere. I'm trying to figure this out, but it is making my head spin... looks like I'm going to have to let the server recover and do some more thinking. Crap.]

[This message has been edited by Suho1004 (edited 03-04-2003).]

[This message has been edited by Suho1004 (edited 03-04-2003).]

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 03-04-2003 09:33

well, I'm at work now, can't really help you (ie. not spend time reading long posts) now,
but if no one picks this up till tonight (call it 9 hours from now), I'll try to help you out ;-)

so long,
TP

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-04-2003 09:49

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-04-2003 09:52

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-04-2003 10:29

InI: Thanks for the ideas, but I must admit that I don't really understand what you're talking about in your first post, especially the part about the variables and the part about the security hole... I will take a look into installing my own Apache (does this mean I would be using my own computer as my server?).

Also, the immediate problem I was having was solved in my second post--the problem was that I had actual subdirectories that had the same names as the "virtual" directories I was trying to use (/writings/ etc.).

The problem with crashing the server really boils down to this: 1) any URL typed into the address bar (or any link clicked) is directed by .htaccess to my handler file; 2) the handler file checks to see if the file really exists, and if it does it includes the actual file; 3) if the file does not exist, it breaks down the URL and determines which actual file it points to; 3a) if the URL corresponds to no actual file, it spits out an error message (a 404, basically); 3b) if it does correspond to an actual file, it includes that file. As far as I can determine, 3b) initiates another call to the handler file, since every server request is processed by .htaccess, right? The problem arises when the various checks I have in my code determine that the URL is valid, but successive calls to the handler file just get stuck in an endless loop, apparently bringing down the server.

I must admit that I don't really understand what I just said, since it doesn't seem to make sense. For example, if I type liminality.org/writings/backstory/1/... 1) the URL goes to my handler file; 2) the handler file sees that this does not point to an actual file; 3) after breaking the URL down the handler file determines that it is pointing to the first page of my background story, and thus 3b) should spit out liminality.org/writings_entry?item=backstory&page=1.

I'm not exactly sure what happens next. The PHP file does exist, but what about the variables I'm passing? Will the handler file pass them through and find the right file. Well, apparently something is going wrong along the way. But if the file doesn't exist and it doesn't match my criteria for my virtual directory structure (which the above URL doesn't), the handler file should spit out the 404 message. The only way it should get stuck in a loop is if the handler file determines that the URL exists and processes it accordingly, but only ends up calling the same thing over and over again. My brain has only a very tenuous grasp on this concept, and I just don't see how that could happen here.

It should be obvious that I don't really understand how the .htaccess/PHP redirect combination works. I tried to follow the ALA article I linked to above, but I am continuously stymied by the fact that whatever is spit out by my handler file goes back into the handler file for reevaluation. This is how things work, though, and I know others must have gotten this to work somehow. If it weren't for the fact that I keep bringing down the server I would just plug away at it piece by piece until I figured it out, but as it is I have no choice but to look at the code and try to think everything through from the beginning (since I don't really have the luxury of testing). Unfortunately, I have too little knowledge of the fundamental technologies and the problem is just too big for me to wrap my brain around.

I suppose I will have to look into installing Apache on my system, that way I can play around without having to worry about harming anyone else. I'm still baffled as to what's going on.

InI/TP: I'm not sure when you guys will be home from work, but most likely our schedules aren't going to afford us too many windows of opportunity when we are online at the same time. I would really like to talk with you guys on Q, but I don't know if that's going to work out...

[Edit: Just got an e-mail from my host, and he says he doesn't think the problems I described took down the server... he's checking my files out on that end, so we'll see what he comes up with on that front. I think I'm just going to wait until I hear from him, and maybe InI or TP, until I try to do anything else.]

[This message has been edited by Suho1004 (edited 03-04-2003).]

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 03-04-2003 13:15

don't include a 'http://' something.
Include the local file, instead of going around the network
->include("http://liminality.org/writings.html");
becomes
include("writings.html");

so long,
TP

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-04-2003 13:43

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-04-2003 15:15

TP: whoa... I can do that? Hmm... of course I can do that. I'm such a dork. You don't think that has anything to do with my difficulties, do you?

InI: Looking forward to it, although God only knows when your free time and my free time will match up next...

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 03-04-2003 22:25

of course it has.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-05-2003 00:47

TP: Thank you for your extensive replies.

You're right, of course... the http:// was what was causing all the problems--that's what was causing the file to call itself over and over again...

I got an e-mail from my host guy, who explained what was happening and why. Fortunately he is very cool and is not coming to kill me. He's even trying to help me out with what I'm trying to do.

I'll definitely drop you and InI a line next time I see you on Q, though, if I continue to have problems. Now that I know what not to do and the server is safe, I can go back to shooting myself in the foot until my remaining toes figure out how to avoid the bullets.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 03-05-2003 09:26

yeah, you'll be able to catch me on Q some time soon...
the last three days at home, I tried to install a new harddrive. Was an Odysee of fear, hate, buggy drivers, even buggier chips (bugger that!), to say the least.
But now it seems to be working all right, so catch you soon.

so long,

Tyberius Prime

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 03-05-2003 13:39

regarding this sort of renamining url things, i noticed alot of mention of .htaccess stuff. I was wondering if its possible to do these things on windows servers where you dont have a .htaccess file as you do in a Unix server? thanks

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-05-2003 14:55

Well, you can't use .htaccess on windows servers, but I do remember reading that there was another way... if I can find that resource I'll post it here.

[Edit: Weird... I know I saw it somewhere. Well, I don't think I'm going to be able to find it before I go to bed tonight. Maybe tomorrow.]

[This message has been edited by Suho1004 (edited 03-05-2003).]

AT
Bipolar (III) Inmate

From: Louisville, KY, USA
Insane since: Aug 2000

posted posted 03-05-2003 17:49

Actually you *can* use .htaccess on windows.
You just have to edit the httpd.

Check out this link. http://www.kuro5hin.org/story/2002/10/1/213723/598
Rather than just 'AuthConfig' I'd put 'All'

cheers

[This message has been edited by AT (edited 03-05-2003).]

[This message has been edited by AT (edited 03-05-2003).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 03-05-2003 18:24

Suho1004: I find the method as outlined in that link rather 'messy' - see the FAQ for a brief overview (it needs expanding as a lot of people are looking into this):

:FAQ:

also see the GN links section (I'll be throwing more in there when I get around to it):

http://development.gurusnetwork.com/links/167/

http://development.gurusnetwork.com/links/585/

I also started one on using htaccess with Windows (as mr.maX outlined a great technique quite a while back):

:FAQ:

[edit: links screwing]

___________________
Emps

FAQs: Emperor

AT
Bipolar (III) Inmate

From: Louisville, KY, USA
Insane since: Aug 2000

posted posted 03-05-2003 18:45

If you think that's messy... You should see my hair...

run



bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 03-05-2003 19:50

One of the issues with .htaccess files on windows is the inabilty of "dot something (.something)" files

This can be worked around by changing the AccessFileName in your httpd.conf (or sometimes commonhttpd.conf)

To something like ht.access and then using ht.access files



.:[ Never resist a perfect moment ]:.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 03-06-2003 02:34

Great links, Emps! As a matter of fact, my host recommended that I use RewriteRules rather than a PHP page to do the redirects, but I'm more familiar with PHP at this point. The RewriteRules do look a lot neater, though--I'll have to do some more research. Now that I've figured out the http:// problem, I do have a working PHP solution, but I haven't gotten around to fine tuning it. I'm wondering if I should just stay with that for now... I guess I'll have to think about it.

[Edit: For those who are following along at home, I'm starting my research with Apache's documentation on mod_rewrite, which I will probably follow with their URL Rewrite Guide (a supplementary doc). After looking at Emps' links and sniffing around a bit, I'm starting to get excited about mod_rewrite... unfortunately I actually have work to do now, so I'm going to have to let this cool for a little while.]

[This message has been edited by Suho1004 (edited 03-06-2003).]

« BackwardsOnwards »

Show Forum Drop Down Menu