Page 1 of 2

PHP Snippets

Posted: Sun May 02, 2004 4:01 am
by RuffRyders
[b]Displaying visitors browser information.[/b]

Simply paste the following code into your pages source:
[code]<?php
   echo $_SERVER['HTTP_USER_AGENT'];
?>[/code]

RuffRyders.

PHP Snippets

Posted: Sun May 02, 2004 4:10 am
by RuffRyders
[b]Displaying Your Servers Information.[/b]

Simply paste the following code into your source.
[code]<?php
  phpinfo();
?>[/code]

RuffRyders.

PHP Snippets

Posted: Sun May 02, 2004 4:24 am
by RuffRyders
Simply paste the following snippets into your pages source:

[b]Displaying a visitors IP address.[/b]

[code]<?php
echo "Your IP Address: ", $_SERVER['REMOTE_ADDR'];
?>[/code]

[b]Displaying your servers IP address.[/b]

[code]<?php
echo "Server Address: ", $_SERVER['SERVER_ADDR'];
?>[/code]

RuffRyders.

PHP Snippets

Posted: Sun May 02, 2004 5:10 am
by RuffRyders
[b]Random Quote.[/b]

Simply paste the following snippet into your pages source.
[code]<?php

$quote[1]="This is quote 1";
$quote[2]="This is quote 2";
$quote[3]="This is quote 3";
$quote[4]="This is quote 4";
$quote[5]="This is quote 5";

$random = rand(1, count($quote));

echo "$quote[$random]";

?>[/code]

RuffRyders.

PHP Snippets

Posted: Sun May 02, 2004 5:13 am
by RuffRyders
[b]Referer.[/b]

Simply tells the visitor which site they previously came from to get to your site.
[code]<?php

echo $_SERVER['HTTP_REFERER'];

?>[/code]

RuffRyders

PHP Snippets

Posted: Sun May 02, 2004 5:25 am
by RuffRyders
[b]Include a page.[/b]

What this does is include a page within another page.
[code]<?php
include "yourpage.php";
?>[/code]

RuffRyders.

PHP Snippets

Posted: Sat May 15, 2004 12:00 am
by False_Pretense
[color=\"blue\"]this is a flat unique ip counter. it writes to two files you will have to make "ips.txt" "logipdates.txt" it uses a loop to count the ips from the ips.txt file. The logipdates.txt is for putting a date and time to the ip hits.

[/color]
[html]<?php $file = "C:/yoursite/text/ips.txt";
$logdate = "C:/yoursite/text/logipdates.txt";
$today = date (" m,d,Y,g:ia");

// Obtain users IP address
$getip = getenv(REMOTE_ADDR);
$addip = "TRUE";
$count = 0;

// Open $file and search each line (IP address) for a match
$fpip = fopen($file,"r");
while (!feof($fpip))
{
$line = fgets($fpip, 4096); //gets one line at a time
$line=trim($line);
if ($line != "")
{
$count++;
}
}

// If IP is already logged
if ($line==$getip)
{
$addip = "FALSE";
}
{
fclose($fpip);
}

// If the IP was not previously logged, append it to $file
if ($addip == "TRUE")
{
$fpip = fopen($file,"a");
fwrite($fpip, "\n");
fwrite($fpip, $getip);
fclose($fpip);
$count++;
$fpd = fopen($logdate, "a");
fwrite($fpdate, "\n");
fwrite($fpdate, $ipadd);
fwrite($fpdate, $today);
fclose($fpdate);
}

// Display count
echo $count;
readfile ("$logdate");
?>[/html]

[i][color=\"darkred\"]Chucked in some [html ] and [/ html] (without spaces), easier to read /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />[/color][/i]

PHP Snippets

Posted: Sun May 23, 2004 8:30 am
by RuffRyders
[b]Displaying Users Hostname.[/b]

[quote]<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $hostname;
?>[/quote]

RuffRyders.

PHP Snippets

Posted: Sat May 29, 2004 7:00 am
by RuffRyders
[b]Simple Calculator.[/b]

[code]<?PHP

if(!$_POST['act'])
{
 echo '
<form method="post" action="'.$_SERVER['PHP_SELF'].'">
 <input type="text" name="z1" size="5">
 <select name="act">
  <option value="add">[ + ]</option>
  <option value="sub">[ - ]</option>
  <option value="div">[ / ]</option>
  <option value="mul">[ x ]</option>
  <option value="pro">[ % ]</option>
 </select>
 <input type="text" name="z2" size="5">
 <input type="submit" name="doit" value="Calculate">
</form>
';
}else{
 if($_POST['act'] == 'add')
 {
  $calc = ($_POST['z1']+$_POST['z2']);
  $op = '+';
 }
 if($_POST['act'] == 'sub')
 {
  $calc = ($_POST['z1']-$_POST['z2']);
  $op = '-';
 }
 if($_POST['act'] == 'div')
 {
  $calc = ($_POST['z1']/$_POST['z2']);
  $op = '/';
 }
 if($_POST['act'] == 'mul')
 {
  $calc = ($_POST['z1']*$_POST['z2']);
  $op = 'x';
 }
 if($_POST['act'] == 'pro')
 {
  $calc = (($_POST['z2']*$_POST['z1'])/100);
  $op = '% of';
 }
 echo $_POST['z1'].' '.$op.' '.$_POST['z2'].' = '.$calc;
}

?>[/code]

RuffRyders.

PHP Snippets

Posted: Sat May 29, 2004 7:32 am
by RuffRyders
[b]Unique ID:[/b]

[code]<?php
  echo $_SERVER['UNIQUE_ID'];
?>[/code]

---------------

[b]Servers administrator:[/b]

[code]<?php
  echo $_SERVER['SERVER_ADMIN'];
?>[/code]

---------------

[b]Displays the port the visitor is connecting through:[/b]

[code]<?php
  echo $_SERVER['SERVER_PORT'];
?>[/code]

---------------

[b]Servers software:[/b]

[code]<?php
  echo $_SERVER['SERVER_SOFTWARE'];
?>[/code]

---------------

[b]The language and country:[/b]

[code]<?php
  echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
?>[/code]

PHP Snippets

Posted: Sun Jun 20, 2004 8:24 pm
by Tami
Originally Posted by MaGiC on GraphixGround March 3 2004

[color=\"red\"]Getting Started [/color]

Create a file named [color=\"blue\"]hello.php[/color] and put it in your web server\'s root directory (DOCUMENT_ROOT) with the following content:

[code]<html>
<head>
 <title>[color=green]PAGE TITLE[/color] </title>
</head>
<body>
<?php echo \'<p>Hello World</p>\'; ?>
</body>
</html>[/code]

Use your browser to access the file with your web server\'s URL, ending with the \"/hello.php\" file reference. When developing locally this URL will be something like [url=\"http://localhost/hello.php\"]http://localhost/hello.php[/url] or [url=\"http://127.0.0.1/hello.php\"]http://127.0.0.1/hello.php[/url] but this depends on the web server\'s configuration. Although this is outside the scope of this tutorial, see also the DocumentRoot and ServerName directives in your web server\'s configuration file (for Apache, this is httpd.conf). If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:

[code]<html>
<head>
 <title>PHP Test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>[/code]

[color=\"red\"]Forms[/color]

[code]<form action="action.php" method="post">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="submit" />
</form>[/code]

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called. In this file you would write something like this:

[code]Hi <?php echo $_POST[\'name\']; ?>.
You are <?php echo $_POST[\'age\']; ?> years old.[/code]

Then the script should show:

Hi Danny. You are 14 years old.

It should be obvious what this does. There is nothing more to it. The $_POST[\'name\'] and $_POST[\'age\'] variables are automatically set for you by PHP. Earlier we used the $_SERVER autoglobal; above we just introduced the $_POST autoglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST autoglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data.

(More Will Be Added Later!)

PHP Snippets

Posted: Tue Jul 27, 2004 11:28 pm
by to@ds
I tried some of RuffRyders codes, the Unique ID, and Display Visitors IP, but couldnt get them to work. Ive not done any PHP before some Im a total newb. I tried putting them in the head, and body of my pages and neither worked. Any help would be great

Thanks

PHP Snippets

Posted: Wed Jul 28, 2004 1:19 am
by Sp3culuM
Try this..

[code]<html>
<head>
<title>PHPness</title>
</head>
<body>
<?
if(getenv(HTTP_CLIENT_IP)) {
         $_ip = getenv(HTTP_CLIENT_IP);
}
elseif(getenv(HTTP_X_FORWARDED_FOR)) {
         $_ip = getenv(HTTP_X_FORWARDED_FOR);
}
else {
         $_ip = $REMOTE_ADDR;
}
echo "Your IP: ".$_ip.".";
echo "Unique ID: ".$_SERVER['UNIQUE_ID'].".";
?>
</body>
</html>[/code]

Also.. are you running these on a PHP enabled server? Silly question I know, but you can't run it locally without speciallist software installed.

PHP Snippets

Posted: Wed Jul 28, 2004 11:47 pm
by to@ds
Ahhh, I thought that might have been the problem. I need some kind of compiler then I take it. Can you name any programs to help? Thanks /bow.gif\' class=\'bbc_emoticon\' alt=\'(bow)\' />

PHP Snippets

Posted: Thu Jul 29, 2004 12:24 am
by Sp3culuM
There's quite a few, appserv and easyphp to name a few. Sorry I don't have the links to the sites any more, thought I did but can't find them /rolleyes.gif\' class=\'bbc_emoticon\' alt=\':rolleyes:\' />