PHP Snippets

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Displaying visitors browser information.

Simply paste the following code into your pages source:

Code: Select all

<?php 
 &nbsp; &nbsp;echo &#036;_SERVER&#91;'HTTP_USER_AGENT'&#93;; 
?>
RuffRyders.
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Displaying Your Servers Information.

Simply paste the following code into your source.

Code: Select all

<?php 
 &nbsp; phpinfo&#40;&#41;; 
?>
RuffRyders.
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Simply paste the following snippets into your pages source:

Displaying a visitors IP address.

Code: Select all

<?php 
echo "Your IP Address&#58; ", &#036;_SERVER&#91;'REMOTE_ADDR'&#93;;
?>
Displaying your servers IP address.

Code: Select all

<?php
echo "Server Address&#58; ", &#036;_SERVER&#91;'SERVER_ADDR'&#93;;
?>
RuffRyders.
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Random Quote.

Simply paste the following snippet into your pages source.

Code: Select all

<?php 

&#036;quote&#91;1&#93;="This is quote 1"; 
&#036;quote&#91;2&#93;="This is quote 2"; 
&#036;quote&#91;3&#93;="This is quote 3"; 
&#036;quote&#91;4&#93;="This is quote 4"; 
&#036;quote&#91;5&#93;="This is quote 5"; 

&#036;random = rand&#40;1, count&#40;&#036;quote&#41;&#41;; 

echo "&#036;quote&#91;&#036;random&#93;"; 

?>
RuffRyders.
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Referer.

Simply tells the visitor which site they previously came from to get to your site.

Code: Select all

<?php

echo &#036;_SERVER&#91;'HTTP_REFERER'&#93;; 

?>
RuffRyders
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Include a page.

What this does is include a page within another page.

Code: Select all

<?php
include "yourpage.php";
?>
RuffRyders.
The bruises fade but memories are made.
False_Pretense
Newbie
Newbie
Posts: 47
Joined: Thu Jun 17, 2004 12:37 am

PHP Snippets

Post 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 &#036;file = "C&#58;/yoursite/text/ips.txt";
&#036;logdate = "C&#58;/yoursite/text/logipdates.txt";
&#036;today = date &#40;" m,d,Y,g&#58;ia"&#41;;

// Obtain users IP address
&#036;getip = getenv&#40;REMOTE_ADDR&#41;;
&#036;addip = "TRUE";
&#036;count = 0;

// Open &#036;file and search each line &#40;IP address&#41; for a match
&#036;fpip = fopen&#40;&#036;file,"r"&#41;;
while &#40;&#33;feof&#40;&#036;fpip&#41;&#41;
&#123;
&#036;line = fgets&#40;&#036;fpip, 4096&#41;; //gets one line at a time
&#036;line=trim&#40;&#036;line&#41;;
if &#40;&#036;line &#33;= ""&#41;
&#123;
&#036;count++;
&#125;
&#125;

// If IP is already logged
if &#40;&#036;line==&#036;getip&#41;
&#123;
&#036;addip = "FALSE";
&#125;
&#123;
fclose&#40;&#036;fpip&#41;;
&#125;

// If the IP was not previously logged, append it to &#036;file
if &#40;&#036;addip == "TRUE"&#41;
&#123;
&#036;fpip = fopen&#40;&#036;file,"a"&#41;;
fwrite&#40;&#036;fpip, "&#092;n"&#41;;
fwrite&#40;&#036;fpip, &#036;getip&#41;;
fclose&#40;&#036;fpip&#41;;
&#036;count++;
&#036;fpd = fopen&#40;&#036;logdate, "a"&#41;;
fwrite&#40;&#036;fpdate, "&#092;n"&#41;;
fwrite&#40;&#036;fpdate, &#036;ipadd&#41;;
fwrite&#40;&#036;fpdate, &#036;today&#41;;
fclose&#40;&#036;fpdate&#41;;
&#125;

// Display count
echo &#036;count;
readfile &#40;"&#036;logdate"&#41;;
?>[/html]

[color=\"darkred\"]Chucked in some [html ] and [/ html] (without spaces), easier to read <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />[/color]
Last edited by False_Pretense on Sat May 15, 2004 1:35 am, edited 1 time in total.
Image
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

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

PHP Snippets

Post by RuffRyders »

Simple Calculator.

Code: Select all

<?PHP 

if&#40;&#33;&#036;_POST&#91;'act'&#93;&#41; 
{ 
 &nbsp;echo ' 
<form method="post" action="'.&#036;_SERVER&#91;'PHP_SELF'&#93;.'"> 
 &nbsp;<input type="text" name="z1" size="5"> 
 &nbsp;<select name="act"> 
 &nbsp; <option value="add">&#91; + &#93;</option> 
 &nbsp; <option value="sub">&#91; - &#93;</option> 
 &nbsp; <option value="div">&#91; / &#93;</option> 
 &nbsp; <option value="mul">&#91; x &#93;</option> 
 &nbsp; <option value="pro">&#91; % &#93;</option> 
 &nbsp;</select> 
 &nbsp;<input type="text" name="z2" size="5"> 
 &nbsp;<input type="submit" name="doit" value="Calculate"> 
</form> 
'; 
}else{ 
 &nbsp;if&#40;&#036;_POST&#91;'act'&#93; == 'add'&#41; 
 &nbsp;{ 
 &nbsp; &#036;calc = &#40;&#036;_POST&#91;'z1'&#93;+&#036;_POST&#91;'z2'&#93;&#41;; 
 &nbsp; &#036;op = '+'; 
 &nbsp;} 
 &nbsp;if&#40;&#036;_POST&#91;'act'&#93; == 'sub'&#41; 
 &nbsp;{ 
 &nbsp; &#036;calc = &#40;&#036;_POST&#91;'z1'&#93;-&#036;_POST&#91;'z2'&#93;&#41;; 
 &nbsp; &#036;op = '-'; 
 &nbsp;} 
 &nbsp;if&#40;&#036;_POST&#91;'act'&#93; == 'div'&#41; 
 &nbsp;{ 
 &nbsp; &#036;calc = &#40;&#036;_POST&#91;'z1'&#93;/&#036;_POST&#91;'z2'&#93;&#41;; 
 &nbsp; &#036;op = '/'; 
 &nbsp;} 
 &nbsp;if&#40;&#036;_POST&#91;'act'&#93; == 'mul'&#41; 
 &nbsp;{ 
 &nbsp; &#036;calc = &#40;&#036;_POST&#91;'z1'&#93;*&#036;_POST&#91;'z2'&#93;&#41;; 
 &nbsp; &#036;op = 'x'; 
 &nbsp;} 
 &nbsp;if&#40;&#036;_POST&#91;'act'&#93; == 'pro'&#41; 
 &nbsp;{ 
 &nbsp; &#036;calc = &#40;&#40;&#036;_POST&#91;'z2'&#93;*&#036;_POST&#91;'z1'&#93;&#41;/100&#41;; 
 &nbsp; &#036;op = '% of'; 
 &nbsp;} 
 &nbsp;echo &#036;_POST&#91;'z1'&#93;.' '.&#036;op.' '.&#036;_POST&#91;'z2'&#93;.' = '.&#036;calc; 
} 

?>
RuffRyders.
The bruises fade but memories are made.
RuffRyders
Hero Member
Hero Member
Posts: 1138
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Unique ID:

Code: Select all

<?php 
 &nbsp; echo &#036;_SERVER&#91;'UNIQUE_ID'&#93;;
?>
---------------

Servers administrator:

Code: Select all

<?php 
 &nbsp; echo &#036;_SERVER&#91;'SERVER_ADMIN'&#93;;
?>
---------------

Displays the port the visitor is connecting through:

Code: Select all

<?php 
 &nbsp; echo &#036;_SERVER&#91;'SERVER_PORT'&#93;;
?>
---------------

Servers software:

Code: Select all

<?php
 &nbsp; echo &#036;_SERVER&#91;'SERVER_SOFTWARE'&#93;;
?>
---------------

The language and country:

Code: Select all

<?php
 &nbsp; echo &#036;_SERVER&#91;'HTTP_ACCEPT_LANGUAGE'&#93;;
?>
The bruises fade but memories are made.
Tami
Administrator
Administrator
Posts: 10892
Joined: Sun Apr 25, 2004 1:05 pm

PHP Snippets

Post 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: Select all

<html>
 <head>
 &nbsp;<title>&#91;color=green&#93;PAGE TITLE&#91;/color&#93; </title>
 </head>
 <body>
 <?php echo \'<p>Hello World</p>\'; ?>
</body>
</html>
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 http://localhost/hello.php or http://127.0.0.1/hello.php 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: Select all

<html>
 <head>
 &nbsp;<title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>
</html>
[color=\"red\"]Forms[/color]

Code: Select all

<form action="action.php" method="post">
 Your name&#58; <input type="text" name="name" />
 Your age&#58; <input type="text" name="age" />
 <input type="submit" />
</form>
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: Select all

Hi <?php echo $_POST&#91;\'name\'&#93;; ?>.
You are <?php echo $_POST&#91;\'age\'&#93;; ?> years old.
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!)
Image

[color=\"#41211C\"]It takes years to build up trust and only seconds to destroy it

[/color]
to@ds
Sr. Member
Sr. Member
Posts: 376
Joined: Thu Jun 17, 2004 12:37 am

PHP Snippets

Post 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
[align=center]Image[/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]
Sp3culuM
Hero Member
Hero Member
Posts: 662
Joined: Mon May 17, 2004 2:36 pm

PHP Snippets

Post 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.
Image
to@ds
Sr. Member
Sr. Member
Posts: 376
Joined: Thu Jun 17, 2004 12:37 am

PHP Snippets

Post 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 <img src=\'http://www.killanet.net/forum3/public/s ... #>/bow.gif\' class=\'bbc_emoticon\' alt=\'(bow)\' />
[align=center]Image[/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]
Sp3culuM
Hero Member
Hero Member
Posts: 662
Joined: Mon May 17, 2004 2:36 pm

PHP Snippets

Post 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 <img src=\'http://www.killanet.net/forum3/public/s ... lleyes.gif\' class=\'bbc_emoticon\' alt=\':rolleyes:\' />
Image
Post Reply

Return to “PHP &amp; MySQL”