PHP Snippets
Moderators: Moderator, Global Moderator
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[b]Displaying visitors browser information.[/b]
Simply paste the following code into your pages source:
[code]<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>[/code]
RuffRyders.
Simply paste the following code into your pages source:
[code]<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>[/code]
RuffRyders.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[b]Displaying Your Servers Information.[/b]
Simply paste the following code into your source.
[code]<?php
phpinfo();
?>[/code]
RuffRyders.
Simply paste the following code into your source.
[code]<?php
phpinfo();
?>[/code]
RuffRyders.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
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.
[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.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[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.
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.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[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
Simply tells the visitor which site they previously came from to get to your site.
[code]<?php
echo $_SERVER['HTTP_REFERER'];
?>[/code]
RuffRyders
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[b]Include a page.[/b]
What this does is include a page within another page.
[code]<?php
include "yourpage.php";
?>[/code]
RuffRyders.
What this does is include a page within another page.
[code]<?php
include "yourpage.php";
?>[/code]
RuffRyders.
The bruises fade but memories are made.
-
False_Pretense
- Newbie

- Posts: 47
- Joined: Thu Jun 17, 2004 12:37 am
PHP Snippets
[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]
[/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
Last edited by False_Pretense on Sat May 15, 2004 1:35 am, edited 1 time in total.

-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[b]Displaying Users Hostname.[/b]
[quote]<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $hostname;
?>[/quote]
RuffRyders.
[quote]<?php
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo $hostname;
?>[/quote]
RuffRyders.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[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.
[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.
The bruises fade but memories are made.
-
RuffRyders
- Hero Member

- Posts: 1138
- Joined: Mon Jun 14, 2004 7:47 am
PHP Snippets
[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]
[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]
The bruises fade but memories are made.
PHP Snippets
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!)
[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!)

[color=\"#41211C\"]It takes years to build up trust and only seconds to destroy it
[/color]
PHP Snippets
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
Thanks
[align=center]
[/align]
[align=center][color=\"#CC0000\"]"You may give a toad a wart, but a toad will never give you a wart"[/color][/align]
[align=center]eighty4toads.co.uk[/align]
[/align][align=center][color=\"#CC0000\"]"You may give a toad a wart, but a toad will never give you a wart"[/color][/align]
[align=center]eighty4toads.co.uk[/align]
PHP Snippets
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.
[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
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)\' />
[align=center]
[/align]
[align=center][color=\"#CC0000\"]"You may give a toad a wart, but a toad will never give you a wart"[/color][/align]
[align=center]eighty4toads.co.uk[/align]
[/align][align=center][color=\"#CC0000\"]"You may give a toad a wart, but a toad will never give you a wart"[/color][/align]
[align=center]eighty4toads.co.uk[/align]
PHP Snippets
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:\' />

