Simple Header Script, Need Help

dquigley

New Member
Messages
249
Reaction score
0
Points
0
So I wrote this pretty simple script that is supposed to set the headers and such depending on your platform, however when I load it it does not show any text at all, however the page IS loading, just nothing shows up. So if you could help me find the error that would be great. Thank you in advance.

<?

$agent = getenv("HTTP_USER_AGENT");

if (preg_match("/Win/i", "$agent")) {
$style = "win";
}
else if (preg_match("/Linux/i", "$agent")) {
$style = "linux";
}

$win_style = "<style type=\"text/css\">p, ul, ol, li
{font-family:Arial;font-size:10pt;font-weight:normal;}
h1 {font-family:Arial;font-size:16pt;font-weight:bold;}
h2 {font-family:Arial;font-size:14pt;font-weight:bold;}
strong {font-family:Arial;font-size:10pt;font-weight:bold;}
em {font-family:Arial;font-size:10pt;font-weight:italic;}
</sytle>";

$linux_style = "<style type=\"text/css\">
p, ul, ol, li {font-family:Times;font-size:12pt;font-weight:normal;}
h1 {font-family:Times;font-size:18pt;font-weight:bold;}
h2 {font-family:Times;font-size:16pt;font-weight:bold;}
strong {font-family:Times;font-size:12pt;font-weight:bold;}
em {font-family:Times;font-size:12pt;font-weight:italic;}
</sytle>";

?>

<HTML>
<HEAD>
<TITLE>Platform Matching</TITLE>

<?
if ($style == "win") {
echo "$win_style";
} else if ($style == "linux") {
echo "$linux_style";
}
?>

</HEAD>
<BODY>

<h1 align=center>This is a level 1 heading</h1>
<h2 align=center>Look! a level 2 heading</h2>
<P align=center>This is a simple paragraph with some
<strong>bold</strong> and <em>emphasized</em> text.</P>

</BODY>
</HTML>
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
The variable $linux_style has to be global, or it will not pass between scripts. Try this:
Code:
global $linux_style;
global $win_style;
global $style;

or just:
global $linux_style, $win_style, $style;
It was quite a headache when I went through the same problem :)
 
Last edited:

dquigley

New Member
Messages
249
Reaction score
0
Points
0
are you sure thats the problem? I looked at it and that doesnt seem correct, the book says to do it this way, and its only one script? I think the problem is actually a misplace ; " () or {} I did it all by the book so im sure I just made a typo.
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
I'm sure, the fact that the variable is printed in a separate script means the variable has to be global.
 

dquigley

New Member
Messages
249
Reaction score
0
Points
0
what do you mean in a seperate script? this is the same file.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
hey, try this:
PHP:
<?php // always start php scripts w/ <?php because some configs might not execute <? as php code

$agent = getenv("HTTP_USER_AGENT");

if (stristr($agent, 'Win'))
{

  $style = "<style type=\"text/css\">p, ul, ol, li
{font-family:Arial;font-size:10pt;font-weight:normal;}
h1 {font-family:Arial;font-size:16pt;font-weight:bold;}
h2 {font-family:Arial;font-size:14pt;font-weight:bold;}
strong {font-family:Arial;font-size:10pt;font-weight:bold;}
em {font-family:Arial;font-size:10pt;font-weight:italic;}
</sytle>";

}
else // If their user_agent doesn't match, then show the linux one (since unix, mac and others could possibly be more similar to linux than windows.
{

  $style = "<style type=\"text/css\">
p, ul, ol, li {font-family:Times;font-size:12pt;font-weight:normal;}
h1 {font-family:Times;font-size:18pt;font-weight:bold;}
h2 {font-family:Times;font-size:16pt;font-weight:bold;}
strong {font-family:Times;font-size:12pt;font-weight:bold;}
em {font-family:Times;font-size:12pt;font-weight:italic;}
</sytle>";

}

?>

<HTML>
<HEAD>
<TITLE>Platform Matching</TITLE>

<?php

echo $style;

?>

</HEAD>
<BODY>

<h1 style='text-align:center'>This is a level 1 heading</h1>
<h2 style='text-align:center'>Look! a level 2 heading</h2>
<p style='text-align:center'>This is a simple paragraph with some
<strong>bold</strong> and <em>emphasized</em> text.</p>

</BODY>
</HTML>
 
Last edited:

dquigley

New Member
Messages
249
Reaction score
0
Points
0
I really need to fix the script I posted, not change it. Because I am doing lessons out of a book I would like to keep it the same that way I can go back later to reference what I did and such. Thank you for helping me and editing it, but I need to find the error I made, however I will note the <?php comment thats interesting.
Thanks for help
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Separate as in a different set of <?php and ?> tags. I had the same problem with a script which loads a visit counter before the HTML and prints it to the page some ways down in the HTML, and this is the same sort of thing. Trust me, try it and tell me if it works =)
 

tttony

Member
Messages
147
Reaction score
0
Points
16
dont use preg_match try with xPlozion code, its better and your code there are some errors, ok you try to learn with a php book but that book its very old I recommend the official php manual
 

dquigley

New Member
Messages
249
Reaction score
0
Points
0
well, where exactly do I put that then?
Edit:
This book isnt old its php6 which is the newest php and it was wrote like this year.
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Thats true, but I he is trying to fix the code, not change it. Thats is a better book though.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
alright, i'll give it another try. couple of things to note.
my use of $_SERVER is because one, its a superglobal, and that getenv works slightly differently than $_SERVER. http://us2.php.net/manual/en/reserved.variables.server.php

http://forums.whirlpool.net.au/forum-replies.cfm?t=778144&r=12276747#r12276747 said:
$_SERVER is a built-in PHP variable. getenv() looks at the current environment.

PHP:
<?

$agent = $_SERVER("HTTP_USER_AGENT");

// first note, preg_match uses more resources than a simple stristr which checks to see if the string exists in the variable, and the i means case-insensitive ('LINUX' === 'linux')
if (stristr($agent, "win")) { // your problem could be the quotes around the variable name.  variables should be bare and not have quotes
  $style = "win";
}
elseif (stristr($agent, "linux")) { // could also be the space between the else and the if. http://us.php.net/elseif
  $style = "linux";
}

$win_style = "<style type=\"text/css\">p, ul, ol, li
{font-family:Arial;font-size:10pt;font-weight:normal;}
h1 {font-family:Arial;font-size:16pt;font-weight:bold;}
h2 {font-family:Arial;font-size:14pt;font-weight:bold;}
strong {font-family:Arial;font-size:10pt;font-weight:bold;}
em {font-family:Arial;font-size:10pt;font-weight:italic;}
</style>";

$linux_style = "<style type=\"text/css\">
p, ul, ol, li {font-family:Times;font-size:12pt;font-weight:normal;}
h1 {font-family:Times;font-size:18pt;font-weight:bold;}
h2 {font-family:Times;font-size:16pt;font-weight:bold;}
strong {font-family:Times;font-size:12pt;font-weight:bold;}
em {font-family:Times;font-size:12pt;font-weight:italic;}
</style>";

?>

<HTML>
<HEAD>
<TITLE>Platform Matching</TITLE>

<?
if ($style == "win") {
echo $win_style; // again, you don't need quotes around variables.
} elseif ($style == "linux") {
echo $linux_style;
}
?>

</HEAD>
<BODY>

<h1 align=center>This is a level 1 heading</h1>
<h2 align=center>Look! a level 2 heading</h2>
<P align=center>This is a simple paragraph with some
<strong>bold</strong> and <em>emphasized</em> text.</P>

</BODY>
</HTML>
 
Last edited:

crisp

New Member
Messages
85
Reaction score
0
Points
0
You might wanna correct the typo's in the style tags too ;)

eg </sytle> to </style>
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
lol, fixed. but if it was not parsing it to the html, then it shouldn't be that. there'll just be an error on the page, but it should still parse
Edit:
Separate as in a different set of <?php and ?> tags. I had the same problem with a script which loads a visit counter before the HTML and prints it to the page some ways down in the HTML, and this is the same sort of thing. Trust me, try it and tell me if it works =)
i've never had any problems with multiple php tags and variables. I do it all the time, where anything php is w/in the tags, then any html is outside, and if there's a string in the html, i'll do a quick <?php echo $string ?>.
I'm sure, the fact that the variable is printed in a separate script means the variable has to be global.
global is used mainly for functions. take this following example into consideration:

PHP:
<?php

function world()
{
  global $str;
  $str .= " world";
}

$str = "Hello";
world();

echo $str; // Hello world

?>

<?php

function world()
{
  $str .= ' world';
}

$str = 'Hello';
world();

echo $str // Hello
 
Last edited:

dquigley

New Member
Messages
249
Reaction score
0
Points
0
HAHAHA, none of those things that you said at first made a difference, the quotes and all that dont matter, I use adobe dreamweaver and it basically shows me what works and what doesnt by color codes.

The problem WAS the misspelled styles, how crazy is that huh!? Thanks guys you are all great for helping ty so much.
 
Top