Use array variable from an includes?

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

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

JAB Creations wrote:What is [$i]? I'm not sure what I'm supposed to do with it? When something works, I replicate it. Once it's replicated I have repetition and once I have repetition I'm able to learn.
Well, I've got a better idea. :lol:

Read the code, understand the code, know what it does, then make use out of it. My little snippet implied that you would run through a for loop (even though, in hindsight, a foreach loop would be better).

The "i" variable is usually used as the default for loop variable in every programming language that I've learned.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragents = array(
  'noajax' => array // $useragents['noajax']
    (
    'Gecko',
    'Opera/3',
    'Opera 3', // Spoofing
    'Opera/4',
    'Opera 4', // Spoofing
    'Opera/5',
    'Opera 5', // Spoofing
    'Opera/6',
    'Opera 6', // Spoofing
    'Opera/7',
    'Opera 7', // Spoofing
    'Opera/8.00',
    'Opera 8.00', // Spoofing
    'Opera/8.01',
    'Opera 8.01', // Spoofing
  ),
);

foreach ($useragents['noajax'] as $a)
{
  // Use your strstr or strpos here
}
?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I am not a developer, I'm a designer. My point of perception is dramatically different from that of a developer. I can't create raw code, only replicate. The only way I can create something unique in any sense is if I have two separate scripts and I have enough understanding to merge them together. That is how I am able to learn web development on my own. It's shallow but programming isn't one of my strengths, design is.
Read the code, understand the code, know what it does, then make use out of it.
It's the same thing with how I've thus far learned JavaScript only the scripts tend to be a little shorter thankfully due to the nature of clientside. For example I did this Google Search to make reference to your post of course go figure such a query is not supported.

I still love learning but if you reply to one of my posts it helps to relate it to something I already understand. :wink: Merging this array with that script I realized after it did not work (as the other script on WebmasterWorld does) that I had to change the array reference to $useragents['noajax']. If I leave Gecko as a value in the array it will tell me in Firefox that Ajax is not supported so this will effectively help me support browsers that AJAX is not supported in. Thanks for the continued help!

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragents = array
(
'noajax' => array // $useragents['noajax']
(
'Opera/3',
'Opera 3', // Spoofing
'Opera/4',
'Opera 4', // Spoofing
'Opera/5',
'Opera 5', // Spoofing
'Opera/6',
'Opera 6', // Spoofing
'Opera/7',
'Opera 7', // Spoofing
'Opera/8.00',
'Opera 8.00', // Spoofing
'Opera/8.01',
'Opera 8.01', // Spoofing
),
);

$checkit = ''; 
$answer = ''; 
foreach ($useragents['noajax'] as $ua) { 
 $checkit = stristr($useragent,$ua); 
 if ($checkit === false) { 
 $answer = false; 
 } else { 
 $answer = true; 
 break; 
 } 
} 

if ($answer) { 
 echo 'Your browser does <b>not</b> support AJAX, falling back...'; 
} else { 
 echo 'Your browser supports AJAX.'; 
} 
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

While that's all fine and dandy, maybe you shouldn't be trying to accomplish that through PHP. Maybe you could use The core functions of AJAX to attempt to create a cookie called "AjaxEnabled" or something, and then later on, check for that cookie.

If the cookie doesn't exist, it's safe to assume that the browser does not support it or they've disabled JavaScript.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

It's unnecessary to serve extensive cookie handling code to the client especially in the absence of overpriced OSX hardware with the issues of stubborn code handling (specifically Safari). It's against my site's TOS to spoof anyway in which case if you are doing weird stuff to expect weird reactions. I have no sympathy for privacy zealots who only believe they know what they're doing.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Riiight...

Anyway, just a suggestion... Considering that any browser that doesn't support AJAX probably will in the future, or does now but hasn't always, you could have the user agents be defined by the browser type and the version, parse the version into an integer, and check against the user's version and the first version of that browser type that is supported.

Not that the current way wouldn't work, but I feel that it'd look neater.
Post Reply