Jump to content


Style Sheet Switcher in PHP


9 replies to this topic

#1 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 22 December 2008 - 09:59 PM

My goal is to create a php script that will find out what browser a user is using. At that time, I would like for php to use if/else statements to inject a particular CSS file into being used for that page based on what client (browser) the user is using at the time.

Basically I want a script that will determine what browser the user has and implement a particular stylesheet (since some browsers see things wrong or not the same). I'm sure this is possible but I just don't know where to start. I've got ideas but I was told by a programmer that it's risky to try creating code without a base to build from.

Please, I wish learn to learn PHP very badly. Whether it be step by step or in one go, can someone show me how to make such a script?

Note: I would like for it to search in particular for the browsers MSIE, FF, and Opera.

I am well aware of IE conditional statements, and I already know how to use them. But I want to learn and master php, I figure this is a good place to start.

In terms of getting everything together I'm not sure where to start. Any help would be appreciated. I did search but never found something like this.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 22 December 2008 - 11:19 PM

The problem with browser detection in php is that it relies upon $_SERVER['HTTP_USER_AGENT'] which can easily be changed by the user, therefore it is not very reliable.

If you want to do it for purely expiremental purposes though, try these searches:
http://www.google.co...owser+detection
http://www.google.co...esheet+switcher

I would like to dispell one myth though: It is risky to program when you don't know what the functions you are using are supposed to do. As long as you avoid system(), unlink(), and other things you aren't familiar with or don't understand after reading the documentation, you will be pretty hard-pressed to actually make a program that could be considered "risky", especially in php. Real programming languages (i.e. C) can produce stuff that breaks things a bit easier.

Edited by rc69, 22 December 2008 - 11:23 PM.


#3 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 23 December 2008 - 09:21 PM

I'll do some searching and thank you. I realize I cannot gain anything without resilience, patience, and time but I must learn some functions as you've stated. The basics are a must for me. I know fluently know XHTML and CSS, I'm hungry for PHP and I realize the learning curve is far steeper. But to me, that's just the reason why I have to master it. It'll be more valuable.

#4 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 25 December 2008 - 11:12 AM

You will need to use Ajax for that. PHP will detect, but it wont be as accurate as AJAX. Now, within the AJAX you can implement a PHP code and use the if/else... But I highly recommend javascript/AJAX for a stylesheet switcher.

#5 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 25 December 2008 - 01:03 PM

Thanks for the reply. I think it would be smarter to tackle one language at a time though...as opposed to trying to learn php, javascript, and ajax all at the same time. Yes, I'm aware that ajax isn't a language.

I will look at JS if I pursue it this way then.

Edited by Raven-X, 25 December 2008 - 01:03 PM.


#6 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 25 December 2008 - 08:55 PM

View PostRaven-X, on Dec 25 2008, 01:03 PM, said:

Thanks for the reply. I think it would be smarter to tackle one language at a time though...as opposed to trying to learn php, javascript, and ajax all at the same time. Yes, I'm aware that ajax isn't a language.

I will look at JS if I pursue it this way then.

Actually, it depends on YOU. I learned PHP and some JS at the same time. You're right that AJAX isn't a lang. it is an offset of JAVASCRIPT. That's all AJAX is

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 December 2008 - 02:29 PM

Sorry, i find it amusing how people like to constantly throw "AJAX" at their problems. Sure, it's good for cleaning stuff around the house, but it can (and in most cases, should be) avoided for most problems.

For instance: A stylesheet switch can be implemented entirely in PHP alone, or JS alone. Mixing the two serves no actual purpose and just wastes bandwidth/server processing time.

Also, AJAX is not an offset of JavaScript, it is a technique:

Wikipedia said:

Ajax, or AJAX (asynchronous JavaScript and XML), is a group of interrelated web development techniques used for creating interactive web applications or rich Internet applications
The truth is, if it wasn't for Google, AJAX probably wouldn't be the term that people use today. JSON was around before AJAX and uses the exact same technique (except that what is returned from the server is not XML, but a dialect of JS that can be passed to eval()).

#8 derek.sullivan

    Jedi In Training

  • Members
  • PipPip
  • 341 posts
  • Gender:Male
  • Location:Georgia
  • Interests:preaching, programming, music, friends, outdoors, moves, books

Posted 30 December 2008 - 03:34 PM

okay because I am a complete idiot and have no clue of what I am talking about right?

Notice in my first response how I suggested to use it for browser detection.

Offset is defined as a balance for something. AJAX is exactly what that is even though it is JAVASCRIPT.

#9 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 31 December 2008 - 01:03 AM

No, i apologize if what i said came off as rude. AJAX is a touchy subject with me as i believe most people are unaware of what it actually is. I have never thought of you as an idiot. I do know that you are semi-new to this stuff and because of that you appear to have come to the same conclusion as most beginners that AJAX is "all-mighty", which it isn't.

I know you suggested JS for browser detection, which is why recommeded using JS alone (to save the extra HTTP request).

I'm sorry again if what i said was offensive.

#10 SatanicPenguins

    P2L Jedi

  • Members
  • PipPipPip
  • 541 posts
  • Gender:Male
  • Interests:Photoshop, Web Design, Graphic Design, Internet.

Posted 01 January 2009 - 08:05 PM

I'd recommend going against ajax, Just because there's really no reason to use it for this. My site uses php to see which color theme they picked and sets the css accordingly.

As i've never messed with browser detection, I probably wont be much help :angrylooking:.

And not bashing rc69, Because that is a good point. But you can't cater to what most would refer to "power users" and changing your user agent will usually put you in that category when dealing with websites, Probably 0.5% will do that (Even on a tech niche website)

Edited by SatanicPenguins, 01 January 2009 - 08:05 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users