Variables: JS to PHP..

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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Variables: JS to PHP..

Post by d3ad1ysp0rk »

I searched first, and found a topic that used a form and wanted to change javascript variables to PHP ones. That user ended up using <input type="hidden"> tags to tranfer it through the form.

Problem is, I want to read the users browser, and activate a PHP script if it isnt internet explorer.

Code: Select all

<html>
<head>
<LINK REL="stylesheet" HREF="style.css" TYPE="text/css">
</head>
<body bgcolor="#000000">
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var navName = navigator.appName;
if(navName == "Microsoft Internet Explorer")
{
   var bro = "yes";
}
//  End -->
</script>
<?PHP
$bro = <script language="javascript">document.write(bro);</script>;
if($bro == "yes")
{
   echo "It works!";
}
else
{
   echo "It doesn't...";
}
?>
</body>
</html>
I would use the get_browser(); function, but EVERY host I've tried it on (3 different ones), dont have it set up properly.. :(

so does anyone have ideas on how to do this?

It'd be even nicer if someone wants to host this for me :D
it's gonna be two small files (subprofile.php and style.css) that probably wont use next to any bandwidth
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

PHP can not read anything client-side... this includes JavaScript.

PHP is server-side, JS is client-side.

The only way you will get JS variables to PHP is to either attach them to the url or post them with a form.

Code: Select all

<head>
<script language="JavaScript">
var navName = navigator.appName; 
if(navName == "Microsoft Internet Explorer") 
&#123; 
    window.location = "file.php?bro=yes";
&#125; 
</script>
</head>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you could test the User-Agent yourself.
Afaik the internet explorer always sends the substring "compatible; MSIE <major>.<minor>";
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Thanks for the idea :)

I decided to redirect the user to another page with JS if their browser is internet explorer (i'll add the rest later). so now you can only load it in a AIM window =)

For now, what is the "or" value in javascript? (ie. || in php)

Code: Select all

<html>
<head>
<LINK REL="stylesheet" HREF="style.css" TYPE="text/css">
</head>
<body bgcolor="#000000">
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var navName = navigator.appName;
if(navName == "Microsoft Internet Explorer")
{
   window.location = "error.php?e=browser";
}
//  End -->
</script>
hey 
<?PHP echo "Welcome to my Subprofile!"; ?>
</body>
</html>
Problem is, I get a blank screen when I load the page in AIM :(

Hey, it's better than "Error: cannot load content" :P

anyone know why?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

|| for javascript as well

Code: Select all

<html>
	<head>
		<script type="text/javascript">
			var i = 1;
			if (i==0 || i==1)
				alert("true");
		</script>
	</head>
	<body>
	
	</body>
</html>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

alright thanks

and it was showing up blank because hostultra doesnt let you access their pages from an IM window (stupid..), but when I put it on my hosting, it worked :)

is there anyway to use PHP to redirect, without headers? (since output has already been sent..)?

or should i use ob_flush(); (which i know barely nothing about :P )
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

Sorry, if I did not read the thread carefully enough but if you want to get the user's browser into your PHP script use:

http://de.php.net/manual/en/function.get-browser.php

or use $HTTP_USER_AGENT only...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Variables: JS to PHP..

Post by volka »

LiLpunkSkateR wrote:I would use the get_browser(); function, but EVERY host I've tried it on (3 different ones), dont have it set up properly.. :(
yes, you haven't ;)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

i run windows ME, 128mb of ram, 12gb HD and a DSL connection (crappy uploads); I can't install apache or host anything :P
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Re: Variables: JS to PHP..

Post by Fredix »

volka wrote:
LiLpunkSkateR wrote:I would use the get_browser(); function, but EVERY host I've tried it on (3 different ones), dont have it set up properly.. :(
yes, you haven't ;)
but still the $HTTP_USER_AGENT has to be available.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

$http_user_agent gives too much information, and it's not always the same with the same browser

(it will output a different thing if you have windows 98 with IE compared to windows XP with IE..)


I've just used Javascript to find the browser (navigator.appName), and then redirected using window.location if it was any browser that isnt AIM :P

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var navName = navigator.appName;
if(navName == "Microsoft Internet Explorer" || navName == "Netscape")
&#123;
   window.location = "error.php?e=browser";
&#125;
//  End -->
</script>
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

$http_user_agent gives too much information, and it's not always the same with the same browser

(it will output a different thing if you have windows 98 with IE compared to windows XP with IE..)
But in both cases you will have "msie" in this variable so preg_match() it for "msie" and no matter what system there is you will get the right browser.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

hmm.. i never thought of doing that.. good idea :)

if my method becomes annoying or something, ill try yours

thanks
Post Reply