Forum Moderators: open

Message Too Old, No Replies

ampersands in URL's

and dynamic css

         

benihana

2:38 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have some aspects of my style sheet generated by php depending on parameters passed in the style @import link.

i want to seperate the parameters passed with & to be valid, but if i do that IE5 wont read the stylesheet.
Any suggestions?

Cheers

benihana

9:01 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*anyone? :(

tedster

9:11 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be one of those spots where you intentionally do not validate for one item - you know why, you know that the document will work well cross-brower, and because browsers are not yet what they should be, you compromise intentionally. It happens. There's no virute in validating if it makes rendering problems.

On the other hand, do you know if IE5 will read the stylesheet when you bring it in with <link> instead of @import? I realize this may give you NN4 troubles, but you might get around that with a browser sniffer.

benihana

9:38 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for replying tedster. youre probably right - ill have to live with it. havent tried <link> but dont really want to get into browser sniffing.

The problem is, we have lots of content editors of varying html ability, and we are trying to push them all towards validation (as apposed to the tag soup that regualrly gets published). This is the only part of our template that doesnt validate, and we kind of need to 'practice what we preach' :(

Hester

11:07 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I may be able to help. I've recently discovered that you can parse all the variables on the end of a web address in one go. Then simply use PHP to split the string down. What this means is that you might be able to do away with the ampersand altogether and hence be valid.

It's also a great trick for shortening the length of the address. Here's exactly what I mean:

Your existing address might be something like this:

file.php?page=intro&style=manilla

This is how I would code it:

file.php?intro-manilla

You can use any symbol you like to separate the variables.

What I've done is scrapped the 'page' and 'style' definitions as you can work those out from the string! Normally you would access the variables like this at the start of the page (in PHP):


$page = $_GET['page'];
$style = $_GET['style'];

You can reference them directly, but this can be a security risk, so it's advised to do it the above way, and set register_globals off in your PHP ini file. (Newer versions of PHP have this off by default.)

Here though is the code required to do away with getting the 'page' and 'style' variables completely:


$all = $_SERVER['QUERY_STRING'];

Now we have everything after the "?" at the end of the address, so it's just a matter of splitting the string by using whatever character you defined to separate the variables - in this case, the hyphen:

list ($page, $style) = split ('-', $all);

So now we have a variable $page that equals "intro" and another $style that equals "manilla". Much simpler!

The only drawback would be if you kept swapping the order of the variables around.

benihana

11:15 am on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks hester.

my colleague is working on a similarilar solution as we type! :)

Hester

1:00 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just thought. Using a hyphen you can still use GET to grab the variables - I imagine PHP will see them as one variable!

My method might allow for ampersands in the variables themselves - at least I think it does. So you could have "Fred&George". I'll do some tests and see if it works.

tomda

1:31 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, but what if data are passed through form?
The result URL will always the usual
php?key1=8&key2=2&key3=520

A bit of topic
I have in a previous topic discussed about a solution to reduce the number of passed variables and it was suggested that I could put all datas in one variable and play with string comparaison (php?var=key8key22key3520) - like Ebay does.

I have tried to implement but got stuck with forms coz it will always return the usual (I know I could do sonething using Javascript - write all data in a hidden text field - but not the best solution)

Cheers

Hester

1:54 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



php?key1=8&key2=2&key3=520

That would become:

php?8-2-520

The code would split the string on the hyphen, giving you the 3 keys.

rocknbil

5:41 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, but what if data are passed through form?

A form method is by post by default (and should be, due to the length limitation of GET) so there would be nothing available in $ENV{QUERY_STRING}.

Furthermore, this requires hard-coding in a particular order that's fine for small projects but over time can be a real burgeon to work around as a project grows. (What number what that one? 22? 23? OOPS it's zero-based . . . )

The original problem **could** be solvable by using the encoded characters for & and = :

key_1%3Dvalue_1%26key_2%3Dvalue_2 . . . .

I say could because I haven't checked it for validation, but if encoded characters validate - voila.