ArcEmu: [Tutorial #1]: Website Basics - ArcEmu

Jump to content

Toggle shoutbox Lastest Announcements

dfighter  : (07 December 2014 - 12:06 PM) Arcemu is in hibernation mode, please read http://arcemu.org/fo...showtopic=26903
dfighter  : (01 January 2013 - 05:56 PM) Arcemu wishes you all a happy new year!
Hasbro  : (12 September 2012 - 10:01 AM) Please excuse our outage from the web! Our web host had a major malfunction!
dfighter  : (01 September 2012 - 04:05 PM) Since the spam bots just don't want to stop, I've enabled admin verification when registering.
dfighter  : (23 January 2012 - 09:56 PM) Please note that from now on you will need to confirm your email on the wiki in order to edit it!
Hasbro  : (31 December 2011 - 12:50 PM) Happy New Years all!
Navid  : (26 December 2011 - 04:09 AM) Merry Christmas !!!!!! Happy holidays all :)
WAmadeus  : (24 December 2011 - 03:54 PM) Merry Christmas to all!
dfighter  : (24 December 2011 - 11:05 AM) The Arcemu team wishes y'all a Merry Christmukkah!
Hasbro  : (05 October 2011 - 12:53 PM) Looking for web designers for upcoming web related project. If you're interested in designing user interfaces contact me
dfighter  : (02 September 2011 - 03:47 PM) So who here wants vehicles in Arcemu? :P http://arcemu.org/fo...showtopic=25440
Hasbro  : (14 August 2011 - 03:25 PM) Join us on irc, grab an irc client and connect to irc.freenode.net join channel #arcemu /server irc.freenode.net:6667 /join #arcemu
jackpoz  : (03 August 2011 - 05:33 AM) to all Lua Engine (old one) users: please check http://arcemu.org/fo...showtopic=25274
Hasbro  : (20 May 2011 - 05:27 PM) Looking for people experienced with CMake configuration and setup! Contact me asap
Hasbro  : (15 May 2011 - 05:03 PM) ArcEmu is recruiting C++ programmers, contact Hasbro if interested.
paroxysm  : (03 May 2011 - 06:26 PM) Updated luabridge gossip example to describe the whole gossip creation process rather than just how to create menu. Gossip tutorial
paroxysm  : (23 April 2011 - 11:35 AM) Lua writers can refer to the Luabridge Tutorials section in the Wiki to learn how to write gossip code correctly.
Hasbro  : (20 April 2011 - 05:22 PM) Thank you for your continuous contribution of bug reports, we are working on them.
Hasbro  : (17 April 2011 - 03:20 AM) Please consider donating to support our bills. Donations can be sent using PayPal to donations@arcemu.org - Thank you for your support.
paroxysm  : (10 April 2011 - 12:43 AM) Refer to the Luabridge Tutorials section in the Wiki to learn the new syntax of luabridge.
Resize Shouts Area

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Tutorial #1]: Website Basics Comes with a server status script!

#1 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 14 September 2009 - 07:30 AM

The Basics

In this section we will make a simple connection script to say whether or not your server is online/offline.

In short, PHP is a scripting language hooked up onto HTML. I guess you could say that PHP is just HTML with more abilities. Because of this, you have to label when your php script starts, and when it ends. To do this, you use php tags. They look like:

<?php
?>


In PHP you can also have variables. These variables can store dynamic data, meaning that the data they contain can change.

<?php
$number = "55";
$number2 = 45;
?>


This defines variable $number as 55. A little heads up about the syntax here: characters and texts must go in quotation marks ( ' ' or " " ), but numbers, true/false, and other variables/functions do not need them. Remember, if you have a mix of things within quotation marks and outside, you need to put a period ( . ) to seperate them. That would look like:

<?php
$number = "55" . $number2;


Another really important part of PHP is the echo/print function. Both have the same purpose: to display things to the browser.

<?php
echo "sweet this text shows up";
print "<br />";
print "More text Oo";
?>


Although echo is more widely used, print is supposedly 'faster'. Of course, since PHP has HTML integrated into it you can have HTML inside of it. For example:

<?php
echo "<b>sweet this text shows up, <i>and</i> it's bolded!</b>";
?>


Now let's back up one more step. Before I showed how to define a variable. But what if I want that variable to show some text? This is what you would do:

<?php
$random = "55";
echo "<b>sweet this text shows up, <i>and</i> it's bolded!</b><br>My Random # is: " . $number;
?>


In you browser this would show up as:

sweet this text shows up, and it's bolded!
My Random # is: 55

If you have variables that are sandwiched in between two quotation marks, you can do this (NOTE: this only works with " ", NOT ' ' )

<?php
$random = "55";
$random2 = "2";
echo "<b>sweet this text shows up, <i>and</i> it's bolded!</b><br>My Random # is: {$number} <br />But my favorite number is {$number2}";
?>


If Else Statements

Pretty nifty, huh? But what would I do if my favorite number was both 2 and 3? How would I make it so that it would say my correct favorite number at the time? This is where things get fun. We will use an IF ELSE statement. Let's look at an example:

<?php
$favorite_number = 2;

if($randomnumber == 2) { 
   echo "My favorite number is 2!";
}

else {
  echo "My favorite number is {$favorite_number}!";
}
?>


Let's see how this script works, and the step it takes.

1. Starts PHP script
2. Defines $favorite_number as 2
3. starts if/else statement
4a. If $favorite_number is 2, then echo: "my favorite number is 2!"
4b. If the $favorite_number is anything OTHER than 2 then echo: "my favorite number is <FAVORITE NUMBER HERE>!"


The if and else are tied together in one one statement. They all run on the same condition - the stuff in the parenthesis ( ) - set by the if. The things in the {} right underneath the if is what happens if the condition is met.

The else is the 'backup'. If the condition isn't met, then the else will be run. Please note that the else is NOT required for an if/else statement. :)

There is also the elseif (not shown above), which is the in between of if and else. Elseif hooks up onto an if with it's own condition. It basically says that if the if's condition isn't met, then check the elseif's condition. The elseif allows to place a more complex if/else statement all in one, instead of multiple. For example:

<?php
$weather = 'Sunny';
if( $color == 'Rain' ) {
  echo "Can't go running outside today :) ";
}
elseif( $color == 'Sunny' ) {
  echo "W00T! Let's go bomb a school!";
}
else {
  echo "Dunno what weather is...";
}


Another thing I'd like to show is that if the line in the {} has only one function, you don't actually need the {}. So, for the thing above, a good php scripter would make it look like:

<?php
$weather = 'Sunny';
if( $color == 'Rain' )
  echo "Can't go running outside today :( ";
elseif( $color == 'Sunny' )
  echo "W00T! Let's go bomb a school!";
else echo "Dunno what weather is...";


Always remember that a smaller script means that the server has less work to do, meaning that the page will load faster (especially when there's hundred's of people accessing the page at the same time)

Connecting to a MySQL server

A good private server site needs to be able to access your database thoroughly. A database is an excellent way to store information, since it stores it in tables efficiently. There are many ways to access databases, but MySQL is probably the best way to do it. To connect to your the database use this script:

mysql_connect("127.0.0.7","root", "root") or die(mysql_error());


This would connect to your localhost database (if you don't know, 127.0.0.1 is your internal IP address which redirects to your localhost), the username root, and with password root. If the connection fails, the connection would close and an error would come up. Now that you're connected to the server, you need to connect to the database. Here's how:

mysql_select_db( "accounts" );


This'll connect to the database named accounts. The next thing you need to know about MySQL is the queries. Basically, this is the structure for a query:

mysql_query( " " );


In the " " goes the normal SQL query syntax. If you don't know how that looks like, here's a few examples to get you on your way:

mysql_query( "SELECT login, gm FROM accounts WHERE acct = '1' LIMIT 1" );


(get the row information of the login and gm fields, from the accounts table, where the acct id is 1, and limit the results to ONE result.

mysql_query( "INSERT INTO accounts VALUES ('maverfax', 'my_pass', 'az' " );


(Insert into the table 'accounts' these values: 'maverfax', 'my_pass', 'az'.)

mysql_query( "UPDATE accounts SET password = 'my_pass2' WHERE login = 'maverfax' OR ip = '127.0.0.1' LIMIT 1 " );


(Update the accounts table by setting the password to 'my_pass2' where the login is 'maverfax' or the ip is '127.0.0.1'. This will only affect one row, because of the LIMIT.)

At the end of the page, for security (and if you have multiple connections opened at the same time - for load time), you will want to close the MySQL connection. You can use this to do so:

mysql_close();


Pulling Everything Together

Okay. So hopefully now you've learned a lot about PHP. Let's do our very first tool using ONLY the stuff that you've learned today.

First let's start our PHP Script, and add in the connection

<?php
$con = mysql_connect("127.0.0.1":"3306","root","root")
or die(mysql_error());
mysql_close();
?>



Next, let's add in an if else statement. We will be using !$con as our condition. In PHP ! means no, not, doesn't work, etc. So in other words... If the connection does not work do this: blah blah blah.


<?php
$con = mysql_connect("127.0.0.1":"3306","root","root")
or die(mysql_error());

if (!$con) {
}
mysql_close();
?>


Now let's make the else statement, incase the server is on.
<?php
$con = mysql_connect("127.0.0.1":"3306","root","root")
or die(mysql_error());

if (!$con) {
} else {  }
mysql_close();
?>



And now for the finishing touches.. add some data in the empty { }.

<?php
$con = mysql_connect("127.0.0.1":"3306","root","root")
or die(mysql_error());

if (!$con) {
?> <img src="img/server-off.png"></img> <? } else { ?> <img src="img/server-on.png"></img> <? }
mysql_close();
?>


updated version:

<?php
//Config Part here...
$host = '127.0.0.1';
$user = 'root';
$pass = 'root';

//Default values for status:
$status = 'off';
$color = 'red';

//Attempt to Connect, mask all errors with @
$con = @mysql_connect( $host, $user, $pass);

//Change $status to on if the server is on
if (!$con) {
   $status = 'on';
   $color = 'green';
}

//Echo the image + status
echo 
 "<img src=\"img/server-{$status}.png\" />
  <br />
  <font color='{$color}'>The server is {$status}!</font>";


And there you go! A perfectly working connection script! B)

This is the first tutorial. The next tutorial will pick up from where I left off here, using slightly more complicated PHP. See you there!

--
Bob
1

#2 User is offline   Navid 

  • Persian Prance of ArcEmu
  • Group: Super Moderator
  • Posts: 718
  • Joined: 30-June 09
  • IRC:Lurker-
  • Gender:Male
  • Location:Iran
  • Interests:PHP,C++,ArcEmu and exactly our lovely community and specially money xD
  • Server OS:Windows

Posted 14 September 2009 - 07:58 AM

Good for PHP bigginer
I'm here to check,lurk and kill you so behave or you will be lurked

Posted Image
0

#3 User is offline   Swiftsmoke 

  • Advanced Member
  • Group: Retired
  • Posts: 152
  • Joined: 21-October 08
  • Gender:Male
  • Location:England

Posted 14 September 2009 - 08:57 AM

just a minor point:

$con = mysql_connect("127.0.0.1":"3306","root","root")
or die(mysql_error());


Wouldnt allow the rest of the script to work IF the sql server is offline, a much better way to do this is the following:

$con = @mysql_connect("127.0.0.1":"3306","root","root");


The @ will suppress any errors and allows the rest of the script to execute, you'll also need to do the following with the mysql_close() too..

@mysql_close($con);


This will only close the new connection we just opened rather than all of them and will also suppress any errors should the connection not be valid.

Posted Image
Posted Image
0

#4 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 14 September 2009 - 04:09 PM

Huh didn't know that. Thanks B)
0

#5 User is offline   carl anton 

  • Newbie
  • Group: Members
  • Posts: 2
  • Joined: 23-February 09
  • Gender:Male

Posted 25 September 2009 - 02:55 PM

hehe nice basic tut :rolleyes:
0

#6 User is offline   Bob Herman 

  • Interested
  • PipPipPipPip
  • Group: Members
  • Posts: 452
  • Joined: 11-October 08
  • Gender:Male

Posted 21 December 2009 - 06:53 PM

I've learned a lot ever since I've made that tutorial. So I've updated it. If you're new with PHP, I'd really, really recommend reading this. *cough* pingue *cough* :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users