Page 1 of 1

Variables: JS to PHP..

Posted: Fri Oct 31, 2003 9:42 pm
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

Posted: Fri Oct 31, 2003 10:20 pm
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>

Posted: Fri Oct 31, 2003 10:35 pm
by volka
you could test the User-Agent yourself.
Afaik the internet explorer always sends the substring "compatible; MSIE <major>.<minor>";

Posted: Fri Oct 31, 2003 10:35 pm
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?

Posted: Fri Oct 31, 2003 10:43 pm
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>

Posted: Fri Oct 31, 2003 11:08 pm
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 )

Posted: Sat Nov 01, 2003 7:17 am
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...

Re: Variables: JS to PHP..

Posted: Sat Nov 01, 2003 8:07 am
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 ;)

Posted: Sat Nov 01, 2003 9:52 am
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

Re: Variables: JS to PHP..

Posted: Sat Nov 01, 2003 2:56 pm
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.

Posted: Sat Nov 01, 2003 3:10 pm
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>

Posted: Sat Nov 01, 2003 3:23 pm
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.

Posted: Sat Nov 01, 2003 9:05 pm
by d3ad1ysp0rk
hmm.. i never thought of doing that.. good idea :)

if my method becomes annoying or something, ill try yours

thanks