PHP Snippets

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

to@ds
Newbie
Newbie
Posts: 0
Joined: Thu Jun 17, 2004 12:37 am

PHP Snippets

Post by to@ds »

Nice one. Im sure I'll get them from cnet or something.
[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]
to@ds
Newbie
Newbie
Posts: 0
Joined: Thu Jun 17, 2004 12:37 am

PHP Snippets

Post by to@ds »

http://www.easyphp.org/ incase anyone wanted to know.
[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]
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

A simple way you could ban a user.

Code: Select all

<?php 
&#036;ip = getenv&#40;"REMOTE_ADDR"&#41;; 
if &#40;&#036;ip =="12.345.678.9"&#41; 
{ 
echo "You're banned from this site &#58;&#41;"; 
exit&#40;&#41;; 
} 
?>
The bruises fade but memories are made.
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

A simple way to echo text for those whom are new to php.

Code: Select all

<?php
echo "Your text here."; 
?>
The bruises fade but memories are made.
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

A Quick log in to Invision based forums for your site.

Code: Select all

<form style="display&#58;inline" action="http&#58;//forums.killagraphix.com/index.php?act=Login&CODE=01&CookieDate=1" method="post">
<input type="text" size="10" name="UserName" onfocus="this.value=''" value="User Name">
<input type="password" size="10" name="PassWord" onfocus="this.value=''" value="ibfrules">
<input type="submit" value="Go">
</form>
Credit to Lightsup55.

If you wish to use this code on your site, for your forums be sure to replace forums.killagraphix.com with your boards url for eg. yourdomain.com/forum/

I know this isn't a php script but I thought I'd paste it here any how.
Last edited by RuffRyders on Sat Oct 30, 2004 11:53 pm, edited 1 time in total.
The bruises fade but memories are made.
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

[color=\"red\"]Seeing how a lot of you use phpbb forums I thought I'd post this:[/color]

Let's take a look at the phpBB table phpBB_users; you'll see a list
of fields. The fields we need are:
-> username
-> user_posts
What we are going to do is:
-> Get username & user's post count from the database
-> Get the next username & user's post count
-> Compare them
-> If the second user's post count is bigger, get put username & post count
into a variable and compare it with the next record.

Code: Select all

<?php 
 &nbsp; &nbsp;&#036;username &nbsp; &nbsp;= 'username'; 
 &nbsp; &nbsp;&#036;password &nbsp; &nbsp; = 'password'; 
 &nbsp; &nbsp;&#036;host &nbsp; &nbsp; &nbsp; &nbsp;= 'localhost'; 
 &nbsp; &nbsp;&#036;connection = mysql_connect &#40;&#036;username, &#036;password, &#036;host&#41;; &nbsp; &nbsp;//Create a MySQL 
connection 

 &nbsp; &nbsp;&#036;query = 'SELECT * from phpbb_users'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Create the query 

 &nbsp; &nbsp;&#036;result = mysql_db_query &#40;&#036;db, &#036;query, &#036;connection&#41;; &nbsp; &nbsp; &nbsp; &nbsp;//Run the query on 
the database 

 &nbsp; &nbsp;&#036;user1_posts &nbsp; &nbsp; = &#036;row&#91;'0'&#93;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Create 2 empty variables to compare 
 &nbsp; &nbsp;&#036;user1_name &nbsp; &nbsp; &nbsp; &nbsp;= &#036;row&#91;'0'&#93;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//the usernames & post counts 

 &nbsp; &nbsp;while &#40;&#036;row = mysql_fetch_array&#40;&#036;result&#41;&#41; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Fetch the results from var 
&#036;result 
 &nbsp; &nbsp; &nbsp; &nbsp;&#036;user2_posts &nbsp; &nbsp; = &#036;row&#91;'user_posts'&#93;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Put the username & post count into 
an temporary variable 
 &nbsp; &nbsp; &nbsp; &nbsp;&#036;user2_name &nbsp; &nbsp;= &#036;row&#91;'username'&#93;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 
 &nbsp; &nbsp; &nbsp; &nbsp;if &#40;&#036;user2_posts > &#036;user1_posts&#41; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Compare the post counts 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#036;user1_posts &nbsp; &nbsp; = &#036;row&#91;'user_posts'&#93;; &nbsp; &nbsp; &nbsp; &nbsp;//If the new one is bigger, replace 
the old one with the new one 
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#036;user1_name &nbsp; &nbsp;= &#036;row&#91;'username'&#93;; 
 &nbsp; &nbsp; &nbsp; &nbsp;} 
 &nbsp; &nbsp;} 

 &nbsp; &nbsp;print &#40;'Top poster&#58; ' . &#036;user1_name . ' with ' . &#036;user1_posts . '.'&#41;; 
?>
That's it&#33;
For questions about this script you can email me at rein3000@hotmail.com
or surf to www.rynusrein.nl.tt.

Taken from: Here
The bruises fade but memories are made.
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Browser Redirect:

Alright, this is a simple little script which will take you to one page if you're using Internet Explorer, and another page if you're using some other browser. Lets decide what this is going to do in English:

If the browser is MicroSoft Internet Explorer (MSIE), then go to spoono.com
If the browser isn't MSIE, then redirect to yahoo.com.

The most essential part of this is that this code has to be sent out before any output to the HTML page. Make sure it is the first line of code on your PHP page. With that said, here is the PHP:

<?
//if its MSIE then
if (&#036;name = strstr (&#036;HTTP_USER_AGENT, "MSIE"))
{
//go to Spoono
Header ("Location: http://www.spoono.com/");
}
else
{
//else go to Yahoo
Header ("Location: http://www.yahoo.com/");
}
?>

Tutorial courtesy of Spoono.
The bruises fade but memories are made.
RuffRyders
Newbie
Newbie
Posts: 0
Joined: Mon Jun 14, 2004 7:47 am

PHP Snippets

Post by RuffRyders »

Use this as a gateway to detect a users accepted language and for example redirect him/her to the right page.

Code: Select all

<?php

function lixlpixel_get_env_var&#40;&#036;Var&#41;
{
 if&#40;empty&#40;&#036;GLOBALS&#91;&#036;Var&#93;&#41;&#41;
 {
 &nbsp;&#036;GLOBALS&#91;&#036;Var&#93;=&#40;&#33;empty&#40;&#036;GLOBALS&#91;'_SERVER'&#93;&#91;&#036;Var&#93;&#41;&#41;?
 &nbsp;&#036;GLOBALS&#91;'_SERVER'&#93;&#91;&#036;Var&#93;&#58;
 &nbsp;&#40;&#33;empty&#40;&#036;GLOBALS&#91;'HTTP_SERVER_VARS'&#93;&#91;&#036;Var&#93;&#41;&#41;?
 &nbsp;&#036;GLOBALS&#91;'HTTP_SERVER_VARS'&#93;&#91;&#036;Var&#93;&#58;'';
 }
}

function lixlpixel_detect_lang&#40;&#41;
{
 // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
 lixlpixel_get_env_var&#40;'HTTP_ACCEPT_LANGUAGE'&#41;;
 lixlpixel_get_env_var&#40;'HTTP_USER_AGENT'&#41;;
 
 &#036;_AL=strtolower&#40;&#036;GLOBALS&#91;'HTTP_ACCEPT_LANGUAGE'&#93;&#41;;
 &#036;_UA=strtolower&#40;&#036;GLOBALS&#91;'HTTP_USER_AGENT'&#93;&#41;;
 
 // Try to detect Primary language if several languages are accepted.
 foreach&#40;&#036;GLOBALS&#91;'_LANG'&#93; as &#036;K&#41;
 {
 &nbsp;if&#40;strpos&#40;&#036;_AL, &#036;K&#41;===0&#41;
 &nbsp; return &#036;K;
 }
 
 // Try to detect any language if not yet detected.
 foreach&#40;&#036;GLOBALS&#91;'_LANG'&#93; as &#036;K&#41;
 {
 &nbsp;if&#40;strpos&#40;&#036;_AL, &#036;K&#41;&#33;==false&#41;
 &nbsp; return &#036;K;
 }
 foreach&#40;&#036;GLOBALS&#91;'_LANG'&#93; as &#036;K&#41;
 {
 &nbsp;if&#40;preg_match&#40;"/&#91;&#092;&#91;&#092;&#40; &#93;{&#036;K}&#91;;,_&#092;-&#092;&#41;&#93;/",&#036;_UA&#41;&#41;
 &nbsp; return &#036;K;
 }
 
 // Return default language if language is not yet detected.
 return &#036;GLOBALS&#91;'_DLANG'&#93;;
}

// Define default language.
&#036;GLOBALS&#91;'_DLANG'&#93;='en';

// Define all available languages.
// WARNING&#58; uncomment all available languages

&#036;GLOBALS&#91;'_LANG'&#93; = array&#40;
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
&#41;;

// Redirect to the correct location.

//header&#40;'location&#58; http&#58;//www.your_site.com/index_'.lixlpixel_detect_lang&#40;&#41;.'.php'&#41;; // Example Implementation
echo 'The Language detected is&#58; '.lixlpixel_detect_lang&#40;&#41;; // For Demonstration

?>
Credit to lixlpixel.
The bruises fade but memories are made.
Post Reply

Return to “PHP &amp; MySQL”