Solved: PHP $_SERVER variable questions

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

doomlord289

Thread Starter
Joined
Nov 1, 2005
Messages
192
What PHP $_SERVER variables store:
1. what browser is being used
2. what OS is being used
3. any other data that might be relivent to a log file

Also, is it possible to use Javascript and/or HTML inside a PHP script? I need Javascript for alerts and HTML in an if...else block to display the actual page.
ex:
PHP:
<?php
	$to = "[email protected]";
	$subject = "Formula Suite Form";
	$name = $_POST['name'];
	$email = $_POST['email'];
	$prgmname = $_POST['prgmname'];
	$maxsize = $_POST['maxsize'];
	$filesize = $_FILES['file']['size'];
	$filename = $_FILES['file']['name'];
    $filetype = strtolower(substr($filename,strrpos($filename,".")));
	$fileext  = ".zip";
	if ( $filetype == $fileext )
	{
		if ( !( $filesize > $maxsize ) )
		{
			if ( move_uploaded_file ($_FILES['file'] ['tmp_name'], "uploads/{$_FILES['file'] ['name']}")  )
      		{
	  			print("Upload Successful!"); //change to a Javascript alert
      		}
			else
     		{
	  			print("Upload Failed!"); //change to a Javascript alert
      		}
		}
		else
		{
			print("File is too large!  Please limit your files to 500KB."); //change to a Javascript alert
		}
		$headers = "From: $email";
		mail($to,$subject,"Name: $name\n\nE-mail: $email\n\nProgram Name: $prgmname\n\nSize: $filesize",$headers);
		$time = date("r");
		$ip = $_SERVER['REMOTE_ADDR'];
		$writeto = "logs/fileupload.txt";
		$text = "\nTime: $time\nName: $name\nE-mail: $email\nIP: $ip\nProgram Name: $prgmname\nFile Size: $filesize\n";
		$handle = fopen($writeto, 'a,t');
		fwrite($handle, $text);
		fclose($handle);
//Put HTML here
	}
	else
	{
		print("File must be in .zip format!"); //change to a Javascript alert
	}
?>
Thanks in advance!
 
Joined
Apr 30, 2005
Messages
3,012
for HTML or JavaScript, you can echo them

Example:

<?php
echo "<a href='http://techguy.org'>TechSupportGuy</a>";
?>

Be careful not to mixup the quotes. Notice how I used single quotes around the url because I already had double quotes around the echo'd statement?
 
Joined
Dec 17, 2004
Messages
606
To get an entier list of server vars avalible to you, make a new php page and add this to it,
PHP:
<?php
foreach ( $_SERVER as $key=>$value )  
{  
echo '$_SERVER['.$key.'] = '.$value.'<br>';  
}
?>
Then you can look through all that and get an idea on what you want to use in the log.

Javascript or html can be used with php since php all takes place before the server sends the page and javascript takes place on browser-side, but like namenotfound said, you can echo any scripts or html you want. Just make sure that if you use " around the echo that you escape the quotes inside the echo like this \" and make new lines like \n and any \'s with \\
 
Joined
Aug 7, 2005
Messages
2,183
php guy said:
...and make new lines like \n...

Just a quick note: You can use <br> as well instead of \n, but \n is probably a better method, for when you come around to forms and emails and stuff, <br> won't work.
 

doomlord289

Thread Starter
Joined
Nov 1, 2005
Messages
192
Thanks for the PHP $_SERVER script php guy.

I figured out how to do the HTML inside PHP, but how do I call a Javascript function? Is it just
PHP:
echo "javascriptfunction()";
where the Javascript function is defined in the head of the document?
 
Joined
Aug 7, 2005
Messages
2,183
Yes... You just have to think of PHP as a before-it-gets-to-you-superduper-thing. By the time the page reaches your browser, PHP has done it's job (and the PHP code is gone). So when you use the echo function, it's just saying "write this to the page."

So then again, yes, because you want javascriptfunction() written to the page.

Note: You could also write the header section and main JavaScript function within the PHP script like this:

Code:
<?php
echo( "<head>JAVASCRIPT STUFF</head>"
?>
...or something similar.
 

doomlord289

Thread Starter
Joined
Nov 1, 2005
Messages
192
Thanks. I think I have all my web development issues out of the way now.
 
Joined
Dec 17, 2004
Messages
606
Eriksrocks said:
Just a quick note: You can use as well instead of \n, but \n is probably a better method, for when you come around to forms and emails and stuff, won't work.
It depends on that kind of page break you want. If you want a break in the actual page that is viewed, then you have to use If you want a break in the lines that php makes, simply pressing enter wont do anything, use \n. For example, if your using php to write to a txt document, using wont do anything, but useing \n will add a new line to the file. 's will always be treated like html 's when echoed and will never add a break to anything but an html page.
 
Joined
Aug 7, 2005
Messages
2,183
php guy said:
It depends on that kind of page break you want. If you want a break in the actual page that is viewed, then you have to use If you want a break in the lines that php makes, simply pressing enter wont do anything, use \n. For example, if your using php to write to a txt document, using wont do anything, but useing \n will add a new line to the file. 's will always be treated like html 's when echoed and will never add a break to anything but an html page.
Yeah I just realized that but I was too lazy to edit the post. :p
Sorry for any confusion that may have caused.
 
Joined
Apr 30, 2005
Messages
3,012
Just want to make a quick point about something.

When I gave the echo example, that's fine for one line of html, just if you're going to use a whole page of it when this might be a little better.

echo <<<HTML
All your code here
HTML;

You can also use other things instead of "HTML" like some people prefer the following:

echo <<<EOF
All your code here
EOF;

It doesn't really matter what word you use, as long as you begin and end with the same word
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top