Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 360voice.com
 XML/APIs
 htaccess/PHP question - URL parsing
 New Topic  Reply to Topic
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  09:55:48 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
Ok, I know a lot of you guys are talented in this and I'm just learning. I tried Google for a solution but was overwhelmed by it and can't find what I'm looking for.

I want to parse an incoming URL (like most of the sigs are done here) and pick up on what the URL had in it, and use those values in the script.

Sounds complicated...but here's an example:

http://stuff.soupyc.com/3vStatus/*VARIABLE 1*/*VARIABLE 2*

I would like this to redirect to http://stuff.soupyc.com/3vStatus/index.php?v1=*VARIABLE 1*&v2=*VARIABLE 2*

Now, I know this is probably quite easy...and I know of at least 2 guys who are doing this (Yes, I'm looking at you TX Eegras and JLees!) but I need a solution for a linux host and PHP.

I've tried several combinations of htaccess files but I can't get the second variable in right. If there was only one, I'd have it done...so I'm thinking I'm missing something simple.

Also, on a side note, it would be nice if I could always return the result of the php script as "3vStatus.png". Again, I know this should be doable...just unsure (and confused as hell - Thank you very much Google!) as to how it's done.

Any help is much appreciated!

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!

TX Eegras

22,426

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 51

Comments Made: 1,337

Senior Gamer

USA
994 Posts

Posted - Jun 10 2008 :  10:02:35 AM  Show Profile  Visit TX Eegras's Homepage  Reply with Quote
What I use is a 404 error page.


ErrorDocument 404 ./404.php


Then I use PHP to split apart the $_SERVER["REFERER"]; variable to get the data I want. I don't know ModRewrite well, so this is easier.


$request = explode("/",$_SERVER['REQUEST_URI']);
print_r($request);


is the code I use for my sig image below. $request is an array that is built off of the request URI (the URL minus domain name), if you're having the script (the 404 page) in a subdomain (I think you are), then you want to ignore $request[0] as it will be the same always.

Hope that helps!

[edit]Added an example.[/edit]


Edited by - TX Eegras on Jun 10 2008 10:06:28 AM
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  10:07:33 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
Ok, I could see that being easy enough!

EDIT: Thanks for the example...made it much easier to understand!

Do you know how I would return a different page name? Or would it automatically keep that name, but just run the script in the 404 page?

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!

Edited by - SoupyC on Jun 10 2008 10:08:33 AM
Go to Top of Page

TX Eegras

22,426

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 51

Comments Made: 1,337

Senior Gamer

USA
994 Posts

Posted - Jun 10 2008 :  10:11:48 AM  Show Profile  Visit TX Eegras's Homepage  Reply with Quote
quote:
Originally posted by SoupyC

Ok, I could see that being easy enough!

EDIT: Thanks for the example...made it much easier to understand!

Do you know how I would return a different page name? Or would it automatically keep that name, but just run the script in the 404 page?

SoupyC



It will still keep the name of the url. If this is just a regular HTML page, then use <title></title>. If you really want to get tricky, we can use a combination of things.

404 error page:

$referer = explode('/',$_SERVER['REFERER_URI']);
header("Location: ./index.php?v1=".$referer[0]."&v2=".$referer[1]);
exit;


Make sure you read http://www.php.net/header well as it gives a lot of information as to why header(); might not work.

which will redirect it to an index.php with v1 being the first subdomain and v2 being the second. I'm pretty sure it also changes the browser's URL bar to index.php?v1=etc.


Edited by - TX Eegras on Jun 10 2008 10:14:21 AM
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  10:16:46 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
Thanks again! That's pretty much what I need!

I've a basic question about .htaccess files now...can you have more than 1 of these on your server?

EDIT: I lied! I used to have an htaccess file on my web root, but I removed it. So it's literally just this one in the folder!

The reason I ask is I already have one on my web host root, and within that I have a stuff.soupyc.com folder, which is basically the contents of the sub-domain.

I put the .htaccess file you specified in that dir, and also the 404.php in there. When I try to look it up...well, I get this: http://stuff.soupyc.com/3vStatIMG/SoupyC

Any ideas? I'm assuming it's because technically it's using a .htaccess to get to stuff.soupyc.com, then another after that...could that be a problem?

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!

Edited by - SoupyC on Jun 10 2008 10:24:52 AM
Go to Top of Page

squidpunch

68,077

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 109

Comments Made: 1,337

360voice Staff

USA
1509 Posts

Posted - Jun 10 2008 :  10:34:39 AM  Show Profile  Visit squidpunch's Homepage  Reply with Quote
each folder on a server can have its own .htaccess, my brain isnt really understanding the rest of what you said - but i blame work overload


*no squids were harmed in the creation or use of this gamertag
Check out the latest Friendcast! | 360friendspot.com | Squidpunch.com | larrabeefamily.com | madden-stats.com
Go to Top of Page

TX Eegras

22,426

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 51

Comments Made: 1,337

Senior Gamer

USA
994 Posts

Posted - Jun 10 2008 :  10:39:44 AM  Show Profile  Visit TX Eegras's Homepage  Reply with Quote
Put the .htaccess file in the subdomain 3vStatIMG and it should work.

[edit]Wait, do you want, say, stuff.soupyc to house all your scripts in the index.php but only run them if v1 is 3vStatIMG?


Edited by - TX Eegras on Jun 10 2008 10:43:03 AM
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  10:46:37 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
I kind of *changed* my subdomain setup there, because it was the reason I was being confused...it may act strangely over the next while.

I'm leaving work now, and heading home. I'll come back to this in a hour and hopefully by subdomain setup will be changed by then.

@squid: I think that's also the reason I can't explain it, lol! I think I have the .htaccess in the right place now so we'll wait and see. Also, I found out my host is doing it's 'own 404 redirection'...I'm wondering if that's playing with it! Will check it out!

@TX Eegras: I'll move that across into there and see if it helps. Oh, and when you say subdomain, you mean folder...right?

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  10:49:20 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
quote:
Originally posted by TX Eegras

...[edit]Wait, do you want, say, stuff.soupyc to house all your scripts in the index.php but only run them if v1 is 3vStatIMG?



No...not exactly, but I thought that was how I maybe had to do it. Do you put your scripts inside the 404.php file in each directory, or how do you do it? Need to do it like the experts!

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!
Go to Top of Page

squidpunch

68,077

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 109

Comments Made: 1,337

360voice Staff

USA
1509 Posts

Posted - Jun 10 2008 :  10:50:38 AM  Show Profile  Visit squidpunch's Homepage  Reply with Quote
i dont know how to do 2 variables off the top of my head but i *THINK* this will do one variable, and properly allow you to call it a PNG. its a bit from memory, so make sure you back up the working .htaccess file if i make your server throw billions of 500 errors due to my stupidity. plus this is a bit shakey of an area for me :)


in your .htaccess file which should be stored in the stuff.soupyc.com directory...

RewriteRule /3vStatus/(.*)\.png /3vStatIMG/index.php?v1=$1


notice that i do not have the same directory name for 3vStatus and 3vStatImg above thats because you dont want the directory to be a valid directory or it will should ignore your rewrite rules and then go to the real directory to check for the files, and any .htaccess rules you defined there



*no squids were harmed in the creation or use of this gamertag
Check out the latest Friendcast! | 360friendspot.com | Squidpunch.com | larrabeefamily.com | madden-stats.com
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  11:10:28 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
OK, I was now prepared to stay at work a bit longer, as they have sorted my subdomains already, and I thought I was getting somewhere...that was until I read that my host (GoDaddy) don't update if you put a NEW .htaccess file in somewhere for up to 30 mins! I'm gonna head home, relax a bit, and come back to it later.

Thanks for all your helps guys...I'll be back!

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!
Go to Top of Page

TX Eegras

22,426

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 51

Comments Made: 1,337

Senior Gamer

USA
994 Posts

Posted - Jun 10 2008 :  11:23:09 AM  Show Profile  Visit TX Eegras's Homepage  Reply with Quote
That's pretty strange for a web host to do that. We'll see if it works in a bit I guess.

Go to Top of Page

a1a1a1

0

Zone: None

Total games played:

Current Streak: 0

Longest Streak: 0

Comments Made: 1,337

Starting Gamer

0 Posts

Posted - Jun 10 2008 :  12:45:14 PM  Show Profile  Reply with Quote
SoupyC - If your host has mod_rewrite enabled, you can use this in your .htaccess
I don't know your exact vhost setup though; but should look something similar to this;

RewriteEngine on
RewriteRule ^3vStatus/([^/\.]+)/([^/\.]+)?$ 3vStatus/index.php?v1=$1&v2=$2 [L]

I'm not 100% sure, but this should work!
Go to Top of Page

TX Eegras

22,426

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 51

Comments Made: 1,337

Senior Gamer

USA
994 Posts

Posted - Jun 10 2008 :  12:56:17 PM  Show Profile  Visit TX Eegras's Homepage  Reply with Quote
quote:
Originally posted by swordfischer

SoupyC - If your host has mod_rewrite enabled, you can use this in your .htaccess
I don't know your exact vhost setup though; but should look something similar to this;

RewriteEngine on
RewriteRule ^3vStatus/([^/\.]+)/([^/\.]+)?$ 3vStatus/index.php?v1=$1&v2=$2 [L]

I'm not 100% sure, but this should work!



I might steal this.

Go to Top of Page

a1a1a1

0

Zone: None

Total games played:

Current Streak: 0

Longest Streak: 0

Comments Made: 1,337

Starting Gamer

0 Posts

Posted - Jun 10 2008 :  1:07:19 PM  Show Profile  Reply with Quote
quote:
Originally posted by TX Eegras
I might steal this.



Go ahead! We all need the help we can get :)
Go to Top of Page

mitharus

13,052

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 23

Comments Made: 1,337

New Gamer

28 Posts

Posted - Jun 10 2008 :  2:01:56 PM  Show Profile  Visit mitharus's Homepage  Reply with Quote
quote:
Originally posted by TX Eegras
404 error page:

$referer = explode('/',$_SERVER['REFERER_URI']);
header("Location: ./index.php?v1=".$referer[0]."&v2=".$referer[1]);
exit;




That works but using PATH_INFO in the PHP page (if usable on your server and it's set) is "easier"** than writing a custom 404.

[**] YMMV, using mod_rewrite may be better, use at your own risk, yadda, yadda, so on... ;)


/*
 * if we have something like:  file.php/something/somethingelse
 * Or even: /somepath/something/somethingelse  (index.php at /somepath/index.php - 
 * letting apache server put it in for us using DirectoryIndex
 * directive)
 */
if(isset($_SERVER['PATH_INFO'])) {
  $count=0;

  /* 
   * str becomes: something_somethingelse -- will fail later just like mod-rewrite
   * would on a '/' char in the passed data, but fails also on the '_' char. 
   * Change to a '|', '!' or some other char to be even more sure if 
   * you may have a '_' in the incoming data
   */
  $str = str_replace("/","_",substr($_SERVER['PATH_INFO'], 1),$count);

  if ($count == 1) {
     /*
      * we replaced one '/', so there's two variables. Started at
      * position '1' in substr(). Get both of them 
      */
     $t = explode('_', $str);
     $v1 = $t[0];
     $v2 = $t[1];
  } else {
     /*
      * Not sure what was sent or failed getting it, 
      * so just go back to the root
      */
     header('Location: /');
  }
}


That may, or may not, work for what's needed, and it gets around having to deal with mod_rewrite and those God awful rules that can make you want to bang your had against a wall when you're staring at pages of em (What, me bitter about the rules and it's syntax? Nahhh).

Edit:
Ohh, forgot to mention. You could get rid of a lot of that checking code and just use split() or preg_split() on the PATH_INFO data.

PS. Hope I didn't over comment that. ;)

Edited by - mitharus on Jun 10 2008 2:08:38 PM
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 10 2008 :  2:09:20 PM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
I guess it would help if I actually left the right .htaccess file on the server while I was waiting, lol!

Have done so now though but I'm gonna play some xbox to keep my streak, get a few achievements, then sleep.

I'll get a look at it in the morning and try to sort it all out! Again guys, many thanks for the help!

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!
Go to Top of Page

SoupyC

101,828

Zone: Recreation

Total games played: 351

Current Streak: 2

Longest Streak: 166

Comments Made: 1,337

Moderator

United Kingdom
2642 Posts

Posted - Jun 11 2008 :  03:29:43 AM  Show Profile  Visit SoupyC's Homepage  Reply with Quote
OK guys...your help has paid off!

I think I have it working exactly how I wanted it. Here's some examples:

http://stuff.soupyc.com/3vStat/test/test2
http://stuff.soupyc.com/3vStat/test

As you can see, it puts the first variable followed by a hypen, and then the second, if present. The php file to do that is located at http://stuff.soupyc.com/3vStatusIMG/index.php, so the redirection works! And, notice the URL stays intact so all is good!

Here's what the .htaccess file looks like:
RewriteEngine on
RewriteRule ^3vStat/([^/\.]+)?$ ../3vStatIMG/index.php?v1=$1 [L]
RewriteRule ^3vStat/([^/\.]+)/([^/\.]+)?$ ../3vStatIMG/index.php?v1=$1&v2=$2 [L]


I used swordfischer's idea, and modified it to allow one OR two variables. It's exactly what I need for my Status image, so expect to see an update which allows custom offsets and dates, with better URL's for use in BBCode (main reason I did it this way!)

I'll also maybe look at the file renaming, but it's not a big priority!

Thanks again for all your help guys!

SoupyC


(See all my API Stuff)


LOVEFiLM - UK Online DVD & Games Rental Service | SoupyC.com - My blog for challenges, gaming news, and more!
Go to Top of Page

a1a1a1

0

Zone: None

Total games played:

Current Streak: 0

Longest Streak: 0

Comments Made: 1,337

Starting Gamer

0 Posts

Posted - Jun 11 2008 :  03:55:15 AM  Show Profile  Reply with Quote
Nice to know it works, then I wasn't completely off :)

mod_rewrite can be such a witch sometimes. And the documentation really sucks! and the sample scripts you find on the internet never does what you want them to :)
Go to Top of Page

mitharus

13,052

Zone: Recreation

Total games played:

Current Streak: 0

Longest Streak: 23

Comments Made: 1,337

New Gamer

28 Posts

Posted - Jun 11 2008 :  12:46:07 PM  Show Profile  Visit mitharus's Homepage  Reply with Quote
quote:
Originally posted by swordfischer
mod_rewrite can be such a witch sometimes. And the documentation really sucks! and the sample scripts you find on the internet never does what you want them to :)


One of my favorite quotes from the online Apache 1.3.x mod_rewrite docs.

"The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail." -- Brian Behlendorf, Apache Group

Personally, I don't think Sendmails config is as bad as a massive Apache config with lots of mod_rewrite rules and conditions. ;)
Go to Top of Page

a1a1a1

0

Zone: None

Total games played:

Current Streak: 0

Longest Streak: 0

Comments Made: 1,337

Starting Gamer

0 Posts

Posted - Jun 11 2008 :  1:16:10 PM  Show Profile  Reply with Quote
Word that!
Well, at least I kinda figured out mod_rewrite some extend.
And I'm never gonna try out sendmail again, god I hate it :)
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Reply to Topic
Jump To:
360voice Community © 2006 360voice.com Go To Top Of Page
Snitz Forums 2000