PHP $_POST variables (oh and DAMNED htaccess)

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
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

hi there guys

long time lurker - first time poster!!

it involves .htaccess so of course - :banghead:

ok - so i am building a site - being all SEO friendly and so on so of course am using the htaccess to turn:

Code: Select all

http://www.mydomain.com/Extra Bit Here
into:

Code: Select all

http://www.mydomain.com/page.php?bit=Extra Bit Here
This, much to my amazement is working 100% perfectly!!! :mrgreen:

Anyway, now I am implementing paging on that, and what i had (pre .htaccess) was that it would go to:

Code: Select all

page.php?bit=Extra Bit Here&page=2
(for page 2 etc)

again, that was working nicely!!

Ok, now what I would REALLY like to do is to change it so that the page number is not shown in the URL but still, obviously set the variable.

I realise that I can't "invisibly" set the $_GET variables, but is there any way of setting the $_POST variables using just a text link? I guess I could use a form to do this, but would seem a little pointless!!

Maybe there is another route to go down?? I would really rather not have to have:

Code: Select all

http://www.mydomain.com/Extra Bit Here/2
for page 2 using htaccess (not that I could get that to work anyway!!!! :banghead: )

Anyway, any help would be gratefully received.

Cheers

The Moose
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

Welcome!

To answer your question: no - .htaccess & redirects can only set properties in the URL which are then interpreted as $_GET variables.

I don't think it's a good idea to even try to make the pagination invisible. It's a similar problem to using frames or Flash - what if the user wants to get to the second, third, or X page? If you kept your pagination invisible, that'd be impossible.

I think a URL like http://www.mydomain.com/ExtraBitHere/2/ is perfectly acceptable.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

ok, so, lets say that I will go ahead and have the links displaying as:

Code: Select all

http://www.mydomain.com/ExtraBitHere/2
I get what your saying about users wanting to choose their page number etc etc.

Maybe you could help out with the .htaccess to do this, as sometimes it will just be:

Code: Select all

http://www.mydomain.com/Extra Bit Here
and sometimes it will have the number too (like above). the .htaccess I have at the moment is:

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) page.php?bit=$1 [NC] 
any ideas on how to get it to work?

I assume something like:

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) page.php?bit=$1&page=$2 [NC] 
but that doesnt seem to work!!

Cheers

The Moose
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

It might be easiest to have 2 rules in the .htaccess. One rule to handle the situation of there being no pagination identifier, and one rule for when there is.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

So, combine the two into something like:

Code: Select all

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) page.php?bit=$1 [NC] 
RewriteRule ^([^/]+)/([^/]+) page.php?bit=$1&page=$2
Cheers

The Moose
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

That's what I'd do. There's probably a way to do it in one expression, but I don't think it matters too much.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

This just creates a redirect loop :-( :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

Put [NC] after your second rule. That should stop the rule from applying if the rule was accessed via a redirect.

I'm wondering why there is a redirect loop at all, as RewriteRule doesn't redirect the user, it's just used to serve up a file different than what was requested.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

oh, and it screws up any images !!!
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

I dont know - a little bit weird!!

I added in the !-f and !-d lines between both rules too. I have [NC] after both rules.

That stopped the redirect loop. But screwed my images and didnt work!!

i think it is seeing the first variable to be Extra Bit Here/2 rather than splitting them, because of the first rule!

The Moose
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

Change your first expression to: ^([^/]+)/$ (note the new trailing slash. Also note the $ at the end. That signifies the pattern should match the entire request, rather than just matching from the beginning of the request, until a slash is found)

As it is, it's matching all requests. You want to restrict it to only match directories.

Comment out the second rule until the first rule works fine. Then move on to the second rule.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

thing is that the first rule is working fine. wihtout throwing this paging nonsense into the mix, it is great!! I was shocked at this when i wrote it and it worked!! its only this damned second rule!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

Hmmm, both your pattern and this pattern ^(.*)?/(.*)$ work fine when trying to match test/2.

Comment out the second rule, then in your page.php file, do a print_r($_SERVER). Maybe the requested URI isn't what you think?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
the moose
Forum Newbie
Posts: 8
Joined: Tue Nov 25, 2008 11:02 am

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by the moose »

it is as expected. I have no idea GRRR like with all htaccess I experience - :banghead: :banghead: :banghead: :banghead: !!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: PHP $_POST variables (oh and DAMNED htaccess)

Post by pickle »

If you require the pagination urls to be like: http://www.mydomain.com/ExtraBitHere/2/, you can change your second rule to also end in a slash, thereby removing the chance it'll match files.

I'm not sure if .htaccess patterns have meta characters, but you might also be able to specify \d to only match numbers in the second set of parentheses.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply