[Tutorial] How to make a changing Avatar for forum

06/26/2007 12:36 NoName#1
Because i always get from time to time pms asking about how do i make the avatar changing all the time i decided to write a little guide, its not that hard:

First you need webspace where you can run something like php and which allows you to use the mod rewrite or to change extensions.

Then you need to write a little script which handles the img output in my case it looks like this:

Code:
$mysql_host = "";
$mysql_user = "";
$mysql_password = "";
$mysql_db = "";
@mysql_connect($mysql_host, $mysql_user, $mysql_password);
@mysql_select_db($mysql_db);
$ran = rand(1,71);
$data = mysql_query('SELECT pic FROM images where id='.$ran);
$wfile = mysql_result($data,0,'pic');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/gif");
$img=file_get_contents('./img/'.$wfile);
echo $img;
it gets a random number between 1-71 which is the number of pictures i have in the database, and get the img and send the context to the browser.
It is important to send the Cache-Control so the browser dont cache that image, otherwise it would always show the same img untill the browser cache expires.

You should maybe builtin a referer check as well, so that your image isnt missused on other sites by other ppl.

Code:
$such = strpos($_SERVER['HTTP_REFERER'], 'elitepvpers.com');
if(isset($_SERVER['HTTP_REFERER']) && $such === false && $_SERVER['HTTP_REFERER']!=''){
 * * * *header("Cache-Control: no-cache, must-revalidate");
 * * * * header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 * * * * header("Content-type: image/gif");
 * * * * $img=file_get_contents('./warning.gif');
 * * * * echo $img;

 * * * * exit;
}
if you want use such a check just put it in front of the other code, in this example it would check if the refer is set and is elitepvpers.com or empty if it doesnt match it would show the file warning.gif and stops the execution of the script.

The reason for the empty referer check is that some ppl use tools or browsers which clears the referer and we dont want to show them the warning.

Next todo is then to activate that the webserver redirect the image to the scripts, which you can define either in the config file of the webserver if you have the access to it or in the .htaccess

Code:
RewriteEngine on
RewriteRule ^image\.jpg$ image.php
this will redirect all [Only registered and activated users can see links. Click Here To Register...] to image.php and execute the script to print out the image.

a more advance version would be for example:

Code:
RewriteEngine on
RewriteRule ^image([0-9]{1,5})\.jpg$ image.php?p=$1
this would redirect image1.jpg to image.php?p=1 so that you can overgive paramters to the files to use 1 file for diff things, i use for example the same file for avatar and signature.
the [0-9] means all numbers between 0-9 are valid and the [1,5] that the number can be 1-5 positions long, so from 0-99999

If a request doesnt match that rule it wont rewrite it, for example image22222222.jpg will just look for that file.
06/26/2007 16:25 Tuxified#2
Pretty nice and very helpfull (:

+k
06/26/2007 20:46 4C1D^#3
nur ein "l" :P
alternativ: man kaufe sich fuer 5 dollar pro tag einen chinesen und zwinge ihn dazu.

ne, aber im ernst. super guide, weiter so ^^*gg*
06/28/2007 23:57 CrankG#4
noch besser wäre es wenn du ein beispiel für kostenlosen webspace hättest wo das klappt :)
und ne deutsche übersetzung.
06/29/2007 08:14 4C1D^#5
ich wette coder benutzt den e*pvp server :P
hmm, gibt ja einige fuer 1 euro, is ja nich das problem ^^ (1 euro, dafuer kann man dann haufenweise crap drauf lagern)
07/13/2007 13:50 NoName#6
ich poste grundsätzlich sowas nur in englisch weil ich mir nicht die mühe mache etwas von ner sprache die eigentlich jeder aber ne gewissen alter können sollte zu übersetzen.

und mich kümmert webspace nicht so sehr da ich eh ne eigenen server seit jahren fahre ;)
07/14/2007 22:24 chin chin#7
Code:
$mysql_host = "";
$mysql_user = "";
$mysql_password = "";
$mysql_db = "";
@mysql_connect($mysql_host, $mysql_user, $mysql_password);
@mysql_select_db($mysql_db);
$ran = rand(1,71);
$data = mysql_query('SELECT pic FROM images where id='.$ran);
$wfile = mysql_result($data,0,'pic');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/gif");
$img=file_get_contents('./img/'.$wfile);
echo $img;
wieso machst du das mit mysql?

mach doch einfach

Code:
$ran = rand(1,2);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/gif");
$img=file_get_contents('./img/'.$ran.'.gif');
echo $img;
und nenn die bilder im img ordner
1.gif
2.gif
3.gif
(...)
:)
07/19/2007 05:17 Aeh'#8
Jop... habe ich mich auch gefrag... naja fukt beides ;D
07/19/2007 19:37 XxAnimusxX#9
Also der Script an sich funzt ja prima, nur habe ich komischerweise Probs mit der htaccess, ich habe im ganzen server-root keine htaccess und dies ist die einzige, mit dem Inhalt wie oben beschrieben jedoch bekomme ich ständig nen Fehler:
"Serverfehler. Überprüfe Deine htaccess-Datei bzw. lösche diese."

Zu testzwecken sehen die verzeichnisse wie folgt aus:
./img_bla/ <- hier liegen die Bilder 1.jpg, 2.jpg usw
./.htaccess <- im root liegt die htaccess mit folgendem Inhalt:
Code:
&#60;Directory &#34;img_bla/&#34;>
RewriteEngine on
RewriteRule ^image&#092;.jpg&#036; ../image.php
&#60;/Directory>
Kann mir jemand weiterhelfen? :>
07/23/2007 15:37 pengpong#10
""Serverfehler. Überprüfe Deine htaccess-Datei bzw. lösche diese.""

Nachdem ich mal nicht denke, dass du da keine tippfehler etc. drin hast, kann es auch gut sein, dass auf deinem server apache einfach kein mod_rewrite geladen hat.
(Oder in der apache config angegeben wurde, dass du mit der .htaccess die Konfiguration nicht ändern kannst. Ich bin mir jetzt da aber grad nicht 100% sicher, ob da die gleiche Fehlermeldung kommen würde)
08/10/2007 00:13 Xandaros#11
Serverfehler!

Die Anfrage kann nicht beantwortet werden, da im Server ein interner Fehler aufgetreten ist. Der Server ist entweder überlastet oder ein Fehler in einem CGI-Skript ist aufgetreten.

Sofern Sie dies für eine Fehlfunktion des Servers halten, informieren Sie bitte den Webmaster hierüber.
Error 500
127.0.0.1
08/10/07 00:12:33
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.2

ich schätz mal das ist der selbe fehler nur in firefox^^ geht es denn nicht ohne, wenn das forum auch php dateien als ava unterstützt? *eigenes hab^^*
08/10/2007 11:47 NoName#12
Quote:
wieso machst du das mit mysql?

mach doch einfach

und nenn die bilder im img ordner
1.gif
2.gif
3.gif
(...)
:)
warum willst du circa 80 bilder umbennen die es bei mir sind und das ist nicht mal nötig. und kann man sicher ohne datenbank machen, mein eigenes script ist circa 4 mal länger und beinhaltet ne ecke mehr.

Quote:
<Directory "img_bla/">
RewriteEngine on
RewriteRule ^image&#092;.jpg&#036; ../image.php
</Directory>
wenn ich das richtig verstehe liegt die php auch im root?

sollte dann eher so aussehen:

Code:
RewriteEngine on
RewriteRule ^image&#092;.jpg&#036; image.php
merke gerade die directory angabe kann weg in htaccess dateien da diese ja bereits im verzeichnis liegen und direkt für dieses gelten (ich nutze selbst keine htaccess files, bevorzuge das aus zu haben für mehr performance ;))

Quote:
wenn das forum auch php dateien als ava unterstützt
das forum unterstützt kein php an sich als avatar, deswegen das umschreiben ^^

und da wird wohl etwas in deinem script falsch sein deshalb die fehlermeldung.