[Tutorial] Creating dynamic signatures using PHP and MySQL.
#1

Hello everybody,

According to my thread: Dynamic Signatures?, I'm here with the tutorial.

What can I use this for?
You can use dynamic signatures to first of all, show your statistics from in-game to everybody else. It can also be used to advertise your server.

What do I need?
  • You need a webhosting which has PHP installed. You can't use homehost, except if you have installed some php server. I won't go any deeper into that because there are enough tutorials about that.
  • I recommend using Notepad ++ to script in.
  • A signature image that you'll use to put the player information in.
  • MySQL Database.
  • I also recommend to use a little php script to get your X/Y coordinates of your picture. You can do without it, but it will take some more time. Click here for more information about X/Y coorindates http://www.emanueleferonato.com/2006...th-javascript/
  • A custom font. --> www.dafont.com <---


(Optional) Part 1: Making "playername input page".
This part is optional as you can do it perfect without it. You can save this page as index.html
Please not that this script doesn't use any design.

This page will ask for a playername and send it to the php page to make an image with the player's stats..

Код HTML:
<html> 
<head>
        <title>My title here</title>
</head>
        <h1>Signature configuration.</h1>
        <p>Hello and welcome to the image configuration page.<br />
        Before you can make your signature, we need in-game name, exact as it is.</p>
       
        <form action="signature.php" method="get"> <!-- When you click the "send" button, this information will be send to the "signature.php" page. If you're using another php page, you need to change "signature.php" to the file you called it. -->
        Playername: <input type="text" name="player_name"><br>  <!-- This is a little text box which will ask for you playername. -->
       <input type="Submit"> <!-- A "send" button. -->
       </form>


</body>
</html>
This very simple html page will look like this:




(Optional) Part 2: Getting playername information from the html page.
We will now work in signature.php. If you're using another name, you need to change the signature.php to the file you called this one. See previous code. ^^
PHP код:
$player_name=$_GET['player_name']; // This gets the player his name from the previous page. 

Part 3: Connecting to MySQL database.
You need your mysql details for this part such as username, password, database and host.
This should be in the same file as part 2.
PHP код:
/* Let's start by configuring your mysql details. */
$username=""//Your MySQL Username.
$password=""// Your MySQL Pass.
$database=""// Your MySQL database.
$host=""// Your MySQL host. This is "localhost" or the IP specified by your hosting company.
/* Next, we will make a connection to the mysql. 
If it can't connect, it'll print on the screen: Unable to select database. Be sure the databasename exists and online is. */
mysql_connect($host,$username,$password); // Connection to the database.
@mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is."); 
Part 4: Protect Mysql injection.
This should be in the same file as part 2.
PHP код:
/* To protect MySQL injection. */
$player_name stripslashes($player_name);
$player_name mysql_real_escape_string($player_name); 
Part 5: Getting player information from the database.
This should be in the same file as part 2.

PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name'"// Gets all the information about the player.
$result=mysql_query($query);
$i=mysql_num_rows($result); // Here we are counting how many rows this result gives us.
/* We will now put the player's information into variables so we can use them more easily. */
/* DON'T FORGET: The names should be exact the same as in your mysql db.*/
if ($i == 1// If the user has been correct, then it'll give us 1 row. If its 1 row, then it'll proceed with the code.
{
    
$Playername=mysql_result($result,0,"Playername"); // Gets the username of the player and put it in the variable $Playername.
    
$Money=mysql_result($result,0,"Money"); // Gets the money of the player and put it in the variable $Money.
    
$Score=mysql_result($result,0,"Score"); // Gets the score of the player and put it in the variable $Score.
/*****ADD PART 6 HERE ******/
} else echo('Username is not in our database. Please try again.'); // If the username doesn't exist (so the row is 0) then it'll give en error. 
We now have all the information about the player. However, we don't do anything with the player veriables.



Part 6: Making png image with the player's variables.
This should be in the same file as part 2.
I use the $text_XXXX variable as I personally think this is easier to work with.

PHP код:
// Creating of the .png image. 
    
header('Content-Type: image/png;'); // Don't touch this. We use this to tell the script we're working with an image.
    
$im = @imagecreatefrompng('mypicture.png') or die("Cannot select the correct image. Please contact the webmaster."); // Don't forget to put your picture there. Eg: playersig.png
    
$text_color imagecolorallocate($im197,197,199); // RED, GREEN, BLUE --> Go to www.colorpicker.com, select a nice color, copy the R/G/B letters provided by colorpicker and put them here.
    
$text_username "$Playername"// This gets the information: player name to be showed in the picture.
    
$text_score "$Score"// Same as above but with score.
    
$text_money "$Money"// Same as above but with money.
    
$font 'myfont.ttf'//Upload your custom font to the directory where this file is placed. Then change the name here.
    /* USAGE OF THE imagettftext: First ($im) shouldn't be changed. (16) is the text-size. (0) is the angle of your text. Change it, and you'll see what's going on. (20) is de X-coordinate of the text.
    (36) is the Y-coordinate of the text. */
    
imagettftext($im1602036$text_color$font$text_username); // Prints the username in the picture. 
    
imagettftext($im1607269$text_color$font$text_score); // Prints the score in the picture.
    
imagettftext($im1607299$text_color$font$text_money); // Prints the money in the picture.
    
imagepng($im);
    
imagedestroy($im); 
This should be at the end of the script. We use this to close the mysql connection, else your server will be overloaded in some time.

PHP код:
mysql_close(); 
Part 7:Combining everything.

PHP код:
<? 
/*
***Made by: Nodroz***
*** Enjoy your signatures! ***
*/
$username=""//Your MySQL Username.
$password=""// Your MySQL Pass.
$database=""// Your MySQL database.
$host=""// Your MySQL host. This is "localhost" or the IP specified by your hosting company.
$player_name=$_GET['player_name']; // This gets the player his name from the previous page.
/* Next, we will make a connection to the mysql. 
If it can't connect, it'll print on the screen: Unable to select database. Be sure the databasename exists and online is. */
mysql_connect($host,$username,$password); // Connection to the database.
@mysql_select_db($database) or die( "Unable to select database. Be sure the databasename exists and online is."); //Selection of the database. If it can't read the database, it'll give an error.
/* To protect MySQL injection. */
$player_name stripslashes($player_name);
$player_name mysql_real_escape_string($player_name);
$query="SELECT * FROM users WHERE Playername='$player_name'"// Gets all the information about the player.
$result=mysql_query($query);
$i=mysql_num_rows($result); // Here we are counting how many rows this result gives us.
/* We will now put the player's information into variables so we can use them more easily. */
/* DON'T FORGET: The names should be exact the same as in your mysql db.*/
if ($i == 1// If the user has been correct, then it'll give us 1 row. If its 1 row, then it'll proceed with the code.
{
        
    
$Playername=mysql_result($result,0,"Playername"); // Gets the username of the player and put it in the variable $Playername.
    
$Money=mysql_result($result,0,"Money"); // Gets the money of the player and put it in the variable $Money.
    
$Score=mysql_result($result,0,"Score"); // Gets the score of the player and put it in the variable $Score.
    // Creating of the .png image. 
    
header('Content-Type: image/png;');
    
$im = @imagecreatefrompng('mypicture.png') or die("Cannot select the correct image. Please contact the webmaster."); // Don't forget to put your picture there.
    
$text_color imagecolorallocate($im197,197,199); // RED, GREEN, BLUE --> Go to www.colorpicker.com, select a nice color. Copy the R/G/B letters provided by colorpicker and put them here.
    
$text_username "$Playername"// This gets the information about player name to be showed in the picture.
    
$text_score "$Score"// Same as above ^^
    
$text_money "$Money"// Same as above ^^
    
$font 'myfont.ttf'//Upload your custum font to the directory where this file is placed. Then change the name here.
    /* USAGE OF THE imagettftext: First ($im) shouldn't be changed. (16) is the text-size. (0) is the angle of your text. Change it, and you'll see what's going on. (20) is de X-coordinate of the text.
    (36) is the Y-coordinate of the text. */
    
imagettftext($im1602036$text_color$font$text_username); // Prints the username in the picture. 
    
imagettftext($im1607269$text_color$font$text_score); // Prints the score in the picture.
    
imagettftext($im1607299$text_color$font$text_money); // Prints the money in the picture.
    
imagepng($im);
    
imagedestroy($im);
} else echo(
'Username is not in our database. Please try again.'); // If the username doesn't exist (so the row is 0) then it'll give en error.
mysql_close();
?>

Part 8: The final result:



Please comment and rate.
If you're encountering any problem, just send me a PM and I'll help you out.
If you see any mistakes in the script, just let me know below.
I hope you like this tutorial as it took me more than an hour to make.


Regards,
Nodroz
Reply
#2

This is a really sweet tutorial!

Well done. Just out of interest. For servers that use INI file systems, could the PHP function parse_ini_file() be used, instead of connecting to a database?
Reply
#3

PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name'"
Shouldn't it be

PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name' LIMIT 1"
Reply
#4

Quote:
Originally Posted by xantowskii
Посмотреть сообщение
PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name'"
Shouldn't it be

PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name' LIMIT 1"
Well, that could be yes. However, you can only have 1 same playername in the database... so I don't think it'll change anything to the result.


Quote:

This is a really sweet tutorial!

Well done. Just out of interest. For servers that use INI file systems, could the PHP function parse_ini_file() be used, instead of connecting to a database?

Sorry, but I don't know how to do this. I'll look up some more information about this.
Reply
#5

I use a free php host to test, i have uploaded .php, .html, picture and font. but when i enter my name ingame, an blank page appears

Edit: now the image appear but no info
Reply
#6

Quote:
Originally Posted by xantowskii
Посмотреть сообщение
PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name'"
Shouldn't it be

PHP код:
$query="SELECT * FROM users WHERE Playername='$player_name' LIMIT 1"
I suggest you to read whats "LIMIT 1" used for since for a while everyone here tends to reply saying it will freeze your server/host if you dont use it, when it doesnt happen and depends on the query you are executing, use a bit of common sense and re read the query again.

Select all from the table 'users' where the field 'Playername' equals to (your user name).

Now try to register the same name two or more times, i dont think so there would be two rows with the same name.

Use limit 1 if, for example, you are selecting the ammount of posts, probably 100 guys with the same post count decide to call that query at the same time, usually happens ; ) or it doesnt?.
Reply
#7

Quote:
Originally Posted by kirk
Посмотреть сообщение
I suggest you to read whats "LIMIT 1" used for since for a while everyone here tends to reply saying it will freeze your server/host if you dont use it, when it doesnt happen and depends on the query you are executing, use a bit of common sense and re read the query again.

Select all from the table 'users' where the field 'Playername' equals to (your user name).

Now try to register the same name two or more times, i dont think so there would be two rows with the same name.

Use limit 1 if, for example, you are selecting the ammount of posts, probably 100 guys with the same post count decide to call that query at the same time, usually happens ; ) or it doesnt?.
LIMIT acts as a INDEX key and it optimizes your Query. If you have 100k accounts it would take long to load the data, LIMIT statement makes it quicker. And not making use of it is really a stupid idea (especially in MyISAM engine).


Quote:
Originally Posted by Nodroz
Посмотреть сообщение
Well, that could be yes. However, you can only have 1 same playername in the database... so I don't think it'll change anything to the result.
LIMIT is used to Optimize the query. I didn't say anything about player name.
Reply
#8

Sweet! I was just thinking about this the other day (And TheKiller removing his tutorial about it ). Thanks a lot!

PS: Did you find that picture on ******? I actually had a signature once which had exactly the same background image, lol.
Reply
#9

Not a bad tutorial on creating dynamic signatures, quite a few people would find it helpful. If I were to suggest something, it would be to add more explanation about the HTML and explain the basics of PHP so that the reader understands what they are doing rather then copying and pasting.
Reply
#10

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
Sweet! I was just thinking about this the other day (And TheKiller removing his tutorial about it ). Thanks a lot!

PS: Did you find that picture on ******? I actually had a signature once which had exactly the same background image, lol.
This has been made by a friend of me. I don't know where he got it from. Could be from ******, however had added the logo and text boxes.


Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Not a bad tutorial on creating dynamic signatures, quite a few people would find it helpful. .
Thank you.

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
If I were to suggest something, it would be to add more explanation about the HTML and explain the basics of PHP so that the reader understands what they are doing rather then copying and pasting.
I guess I've already explained a lot of stuff what that the specified line of the script is used for. Maybe it isn't clear enough for some people ?
Reply
#11

Quote:
Originally Posted by ******
View Post
PLEASE learn when to use "string" and 'string' - that PHP code makes me cringe!
I don't see any problem in my script so far? Do you?
Reply
#12

They both means just the same you're right. I'm not that experienced with php either, so any additional is welcome! I shall change it as soon as possible. Thank you.
Reply
#13

awesome work..
Reply
#14

I must try this, awesome job
Reply
#15

I am totally not experienced with this stuff on PHP, but how do you find the correct coordinates to place the information at, on the image?
Reply
#16

If i use this i must have MySql system saving accounts or i can have .ini?
Reply
#17

Maybe you can create a already done script, how many times i try, it gives mes error :/
Reply
#18

Quote:
Originally Posted by No Fear
View Post
Maybe you can create a already done script, how many times i try, it gives mes error :/
What errors are you getting ?



Quote:
Originally Posted by Speed
View Post
If i use this i must have MySql system saving accounts or i can have .ini?
This is only for MySQL so far. Sorry.


Quote:
Originally Posted by Biesmen
View Post
I am totally not experienced with this stuff on PHP, but how do you find the correct coordinates to place the information at, on the image?
You have 2 options:
1. Do it manual. This mean you type in some coordinates and see where it shows. Then you slowly adjust the coordinate until it's in the place. This might take some time.

2. Using a little php or javascript. You can see more information about this here.
Reply
#19

i got first page working where i enter my name,
and the 2 page i got some trouble, pic load but no text appears. what can be wrong? how to fix?
Reply
#20

Are you loading the correct information from your mysql database ?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)