Suggestions for PHP 6

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Suggestions for PHP 6

Post by lovelf »

PCSpectra wrote:
easily, not like the methods we can use now for this
You mean checking in JS and AJAX'ing the details to the server? :P

It's actually pretty simple. The only inherent problem is, upon first visit, the system cannot know whether the user has it enabled/disabled so you can optionally wait until they click a link to "enter" your site or redirect them auto-magically and I'm not sure how SE's would handle that.

If your site is that dependent on JS/Flash I would suggest you reconsider your design as accessibility is probably out the window and SEO is probably not a concern for you, in which case, a redirect might be a good solution. :)
No, see simple PHP Javascript detection below:

Code: Select all

<?php $jp = $_POST['jpb']; ?> 
<?php if($jp == ""){echo "<script type=\"text/javascript\">document.write('<form style=\"display:none;\" method=\"post\" name=\"jp\"><input type=\"hidden\" name=\"jpb\" value=\"y\"></form>'); document.jp.submit();</script>";} ?>
<?php
if($jp == ""){whatever you want for no javascript users, variables you can store with values for no javascript users, functions for no javascript users, etc a php include for the noscript users...}
else {all stuff for javascript enabled browsers} ?>
 
Even though is simple it has a small? problem, if the user goes into any document inside the site and hits the back button there is the: "do you want to resubmit the info pop up alert for javascript users that got the form sent"

Does anyone know of a workaround to this?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Suggestions for PHP 6

Post by Weirdan »

It's no different from redirect, only more noisy, as you noticed yourself. Here's implementation that does not have this problem:

Code: Select all

 
session_start();
if (!array_key_exists('jsEnabled', $_SESSION)) {
   // first hit, detect js
   echo "<script type='text/javascript'>window.location='{$_SERVER["SCRIPT_NAME"]}?jsEnabled=1';</script>";
   $_SESSION['jsEnabled'] = false;
} else if (($_SESSION['jsEnabled'] === false) && !empty($_GET['jsEnabled'])) {
   // redirect to get rid of '?jsEnabled=1' in url
   header("Location: {$_SERVER["SCRIPT_NAME"]}");
   $_SESSION['jsEnabled'] = true;
   die();
}
 
 
if ($_SESSION['jsEnabled']) {
    // content for js enabled browsers
} else {
    // content for browsers with js disabled
}
 
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Suggestions for PHP 6

Post by lovelf »

Weirdan,

I tried the code above with the addition:

Code: Select all

 
 if ($_SESSION['jsEnabled']) {
$ler = "javascript enabled";
} else {
$ler = "javascript disabled";
}
echo $ler;
 
But with javascript disabled I would still see enabled, what am I doing wrong?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Suggestions for PHP 6

Post by Weirdan »

lovelf wrote:But with javascript disabled I would still see enabled, what am I doing wrong?
Remember that the detection works only on the first hit. If you tried it first with js enabled and than with js disabled, the fact that you had js enabled is remembered in SESSION. After testing it with js enabled remove cookies that the page has set to your browser and try again with js disabled.

I think people seldom disable javascript while browsing a site, so this situation is not handled by this snippet.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Suggestions for PHP 6

Post by kaisellgren »

Even the User Agent -header is optional. The web browser does not need to send it. You are a lucky boy if you even get that, not to talk about screen resolutions, etc. That does not really make any sense. PHP is a server side software, not a client side. Use JavaScript if you want that information.

Btw, on one of my websites (30 000 unique visitors a month), 99.86% of visitors have JavaScript enabled. Just a reminder, we live in a year 2009. Did anyone think about that? :roll:

EDIT: $_SESSION can work without cookies by submitting the value into the URL (GET).
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Suggestions for PHP 6

Post by lovelf »

But if we could get screen resolution with PHP then we could serve the client better code cooked server side.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Suggestions for PHP 6

Post by kaisellgren »

lovelf wrote:But if we could get screen resolution with PHP then we could serve the client better code cooked server side.
PHP is capable of doing it. Why wouldn't it? The problem is that no web browser submit such information.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Suggestions for PHP 6

Post by josh »

See whats being overlooked is the fact that javascript is presentation logic. Most ( I would hope ) would agree making 1 app that works with or without jscript is easier then trying to maintain two apps where anytime business logic changes you have to update both the jscript and non jscript versions. Why not just make it degrade gracefully. Use <noscript> tags instead of detecting with PHP.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Suggestions for PHP 6

Post by lovelf »

The thing with noscript tags is that there is more code sent to the client and sometimes the code does not validate on the W3C validator.
Is alright, it is just not possible now, that is why I suggested if there might be a way to make this available - regardless of the little importance it might have to some, since it could be important for others - on PHP6.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Suggestions for PHP 6

Post by kaisellgren »

lovelf wrote:The thing with noscript tags is that there is more code sent to the client and sometimes the code does not validate on the W3C validator.
Is alright, it is just not possible now, that is why I suggested if there might be a way to make this available - regardless of the little importance it might have to some, since it could be important for others - on PHP6.
God please...

PHP team should not do anything about this kind of stuff - at least not for as long as there are no standards about this.

Besides, if a browser sends any such information, it is already fetchable in PHP:

Code: Select all

$raw = file_get_contents("php://input");
That contains the raw data that was sent to the server (including possible screen resolutions, etc if such info was ever sent).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Suggestions for PHP 6

Post by Weirdan »

kaisellgren wrote: Besides, if a browser sends any such information, it is already fetchable in PHP:

Code: Select all

$raw = file_get_contents("php://input");
That contains the raw data that was sent to the server (including possible screen resolutions, etc if such info was ever sent).
Actually that would return request body - unlikely that it will contain such data. Result of apache_request_headers() is much more likely to contains such information.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Suggestions for PHP 6

Post by alex.barylski »

Btw, on one of my websites (30 000 unique visitors a month), 99.86% of visitors have JavaScript enabled. Just a reminder, we live in a year 2009. Did anyone think about that?
1. My Blackberry has JS disabled by default. I assume that will remain the norm for mobile devices for some time to come.

2. 99.9% of your users will never exploit your web site either, does that make it right to neglect the remaining 1% who will?

I used to always use JS until I finally heard enough from other developers on here harping about building software that doesn't 'rely' on JS. Once I made the transition, I have to admit it led to a cleaner, lighter, faster, more reliable and stable application. I still use javascript extensively (AJAX, WYSIWYG, etc) but my applications do no rely on it, at least for critical funcitonality. Like when using a WYSIWYG the fallback is a plain text editor but it still remains usable, unlike a drop down menu which when disabled prevents a user from navigating a web site or application.

Cheers,
Alex
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Suggestions for PHP 6

Post by Benjamin »

Here's a suggestion. Fix this crap:

viewtopic.php?p=525167#p525167
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Suggestions for PHP 6

Post by kaisellgren »

PCSpectra wrote:1. My Blackberry has JS disabled by default. I assume that will remain the norm for mobile devices for some time to come.

2. 99.9% of your users will never exploit your web site either, does that make it right to neglect the remaining 1% who will?
And? That is no problem, you just fall back and do not collect statistics for those visitors.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Suggestions for PHP 6

Post by crazycoders »

Chris Corbyn wrote:Anonymous objects...
snipped out anonymous methods code
Unless wrong, this is part of php6 along with lambda expressions no?
Chris Corbyn wrote:Accessor synthesizers...
snipped out Accessor synthesizers code
Accessor synthesizers are simply get/set methods that are public but exist or not. If you wish to protect a variable from being armed in an object simply create a public function getvariable(){ return $this->privatevariable; } and don't create a setter.
I add this to my base object class that i always use:

Code: Select all

 
class system_object {   
 
    //Constructor of the class
    public function __construct(){}
    
    //Magic property handlers
    public function __get($name){
        if(method_exists($this, 'get'.$name)){ return call_user_func(array($this, 'get'.$name)); }
        else{ throw new Exception('Property '.$name.' not found in '.$this.' ('.get_class($this).') or property is writeonly.'); }
    }
    public function __set($name, $value){
        if(method_exists($this, 'set'.$name)){ call_user_func(array($this, 'set'.$name), $value); }
        else{ throw new Exception('Property '.$name.' not found in '.$this.' ('.get_class($this).') or property is readonly.'); }
    }
 
}
 
Post Reply