Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP]Basic's !

Custom Title Activated
Loyal Member
Joined
Feb 9, 2005
Messages
2,013
Reaction score
6
I dont really know if any of you are interested in PHP, but here are the basics of learning the echo() statements, else() statements, and variables ($) .

--------------------------------
What is needed for this
tutorial?
--------------------------------
1. PHP 4.3.0 or higher
2. A webserver

--------------------------------
Before beginning to script
--------------------------------
IMPORTANT! Remember to always start with php tags before you start scripting any php scripts.

<? is to open a script, and ?> is to close it.

--------------------------------
What are echo statements?
--------------------------------

echo statements are what prints on the pages.

echo statements are pretty useful when coding forms, login scripts, or basically "all" php scripts.


--------------------------------
Lets get started!
--------------------------------

Make a new, flat php document. I use dreamweaver since it already provides the html codings when i create a new document:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

Now between the < body > and < /body > tags, enter something like:

Code:
<?
echo "BreezE is the best";
?>

And save it. (I called mine breeze.php).

Now open your browser. Something like and you can see that the echo statements we just did just printed out like so:



--------------------------------
What are variables?
--------------------------------

Variables are widgets used to temporarily store values.


--------------------------------
Lets get started!
--------------------------------

Sure, variables look hard to do, but its nothing close as hard as doing "else" statements. Variables should be easy once you actually understand what it does.


Create a new php document if you want, (breeze2.php)

Between the body tags, enter like so:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<?

// Create the variables

$name = 'BreezE';
$date = '4/3/05';
$hobbies = 'PHP Scripting';
$book = 'Go Ask Alice';
$d2server = 'Rebornz';

// Print the values
echo "$name has read the book called $book in $date. He actually said it reminded him of a Diablo II server called $d2server. His hobbies are $hobbies.";

?>

</body>
</html>

Now save it as a .php (I called mine BreezE.php once again)

And open it up in an Internet Browser.

Here is how it should look like:


You can see that the variables we made were printed out in the echo statements.

Now, wasnt that easy?

Its time to go to a much harder step.. the "else" statements..

--------------------------------
What are else statements?
--------------------------------

Its a bit difficult to explain, but it is a code that executes when something else is either not defined, defined, or was idle.


--------------------------------
Lets get started!
--------------------------------

Ill make a html form so it makes it easier to understand. (Call this breezeform.php if you want.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="gender" method="post" action="breeze2.php">
  Gender:
   <input type="radio" name="gender" value="M" />
   <strong>Male</strong> 
   <input type="radio" name="gender" value="F" />
   <strong>Female</strong>
   <input type="Submit" name="Submit" value="Submit">
</strong>
</form>
</body>
</html>


We can see that where it says action="breeze2.php" goes directly to breeze2.php and executes the php actions in that file.


Make a new php document. (breeze2.php)

And enter in these codes between the body tags:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<?

if ($gender == 'M') { // If they checked the "Male" button, it prints out:
   echo '<b><p>Good day, Sir!</p></b>';
** elseif ($gender == 'F') { // If they checked the "Female" button, it prints out:
   echo '<b><p>Good day, Madam!</p></b>';
** else { // If they checked neither, it prints out:
   echo '<p><b>You forgot to enter your gender!</b></p>';
**

?>

</body>
</html>

I pretty much explained what the else statements does in the code box.. where i put the // tags

Also, I have to admit why we need those ** and { tags there, but a lot of people/websites say you need them to work something in your php script.

Here are some screenshots to how this else statements work:




If you chose Female, it would print out: "Good day, Madam!"

Now if you DIDN'T choose a gender and pressed Submit:,


it would print out:


Well.. this is pretty much it for my tutorial ;) Have Fun!


-----------------------------------------------------------------

And when im still in PHP i will post the PHP code for stopping SQL Injection:


Code:
<?php
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
**
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
**
return $value;
**

// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());

// Make a safe query
$query = sprintf("SELECT * FROM users WHERE user=%s AND password=%s",
quote_smart($_POST['username']),
quote_smart($_POST['password']));

mysql_query($query);
?>

Ive seen many ask about it so now here it is ;)

Also post youre ideas, basics or tutorial.
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Feb 9, 2005
Messages
2,013
Reaction score
6
matrux said:
very nice man i like it :)
Thanks Mate :icon6:
Im glad someone epreciats it ;)
 
Last edited:
Custom Title Activated
Loyal Member
Joined
Nov 28, 2004
Messages
2,236
Reaction score
0
really nice.
since I am new in PHP it helped me a bit. I knew bout very basics (like ECHO) but now I know how to use IFstatements :)
..and I didn't understand what's for SQL Injection...
 
Custom Title Activated
Loyal Member
Joined
Feb 9, 2005
Messages
2,013
Reaction score
6
[lexx] said:
really nice.
since I am new in PHP it helped me a bit. I knew bout very basics (like ECHO) but now I know how to use IFstatements :)
..and I didn't understand what's for SQL Injection...

Hehe im happy i could help somebody :)

The SQL Injection is mostly posted here for the games like MU so hacking ppl cant vault blast etc ;)
 
Newbie Spellweaver
Joined
Nov 16, 2004
Messages
5
Reaction score
0
Why use **? o_O I've always used ** to close my statements. *edit* WTF @ the forum adding **? o_O

While I'm here, have a while() loop to count to 10 (amazing -.-)

Code:
<?
//Declare a variable to store numbers, called i for integer ;) - set to 1
$i = 1;

//Start the loop
while($i < 11)
{
     echo "$i ";
**
$i++;
?>

Very simple. It basically takes $i, which starts off as 1, and prints it to the screen before adding 1 to it, printing it again, and so on while it's less than 11 (ie. 10). When it gets to 10 it won't go any higher because that wouldn't be 'less than' 11. You can add various if/else statements inside loops.
 
Custom Title Activated
Loyal Member
Joined
Mar 9, 2004
Messages
2,425
Reaction score
2
here is a small sniplet of code from my anime database script, this will list all anime records in my database and link to them

Code:
<? 
//connect to mysql
mysql_connect("localhost","USERTBH","PASSWORD");
	
//select which database you want to edit
mysql_select_db("DBNAME"); 

//If cmd is not hit
if(!isset($cmd)) 
{
   //~~~~~~~~~~~~~~~~~~~~
   $result = mysql_query("select * from dbORDER BY anime_name ASC"); 
   
   //~~~~
   while($r=mysql_fetch_array($result)) 
   { 
      //moo
      $anime_name=$r["anime_name"];//take out the title
      $id=$r["id"];//take out the id
     
      echo "<a href='series.php?id=$id'>$anime_name</a>";
      echo "<br>";
    **
**
?>

this can be edited to list like for instantce the last 5 animes inserted into the DB with a bit of tweakin~
 
Custom Title Activated
Loyal Member
Joined
Sep 7, 2004
Messages
1,886
Reaction score
5
Great Guide for people who dont know PHP like me lol
 
Newbie Spellweaver
Joined
Oct 31, 2004
Messages
91
Reaction score
1
Awesome Guide!
 
Last edited:
Junior Spellweaver
Joined
Dec 21, 2004
Messages
152
Reaction score
0
duck i jsut dunno php...but i know javascript =D
 
Experienced Elementalist
Joined
Apr 18, 2005
Messages
254
Reaction score
0
Well, very nice guide. No comments...
 
Custom Title Activated
Loyal Member
Joined
Nov 19, 2003
Messages
1,174
Reaction score
0
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

<?

// Create the variables

$name = 'BreezE';
$date = '4/3/05';
$hobbies = 'PHP Scripting';
$book = 'Go Ask Alice';
$d2server = 'Rebornz';

// Print the values
echo "$name has read the book called $book in $date. He actually said it reminded him of a Diablo II server called $d2server. His hobbies are $hobbies.";

?>

</body>
</html>

Would it not be better to concatenate, simply sticking variables inside a quotation is a no! It reduces the efficiency and speed greatly, you will see the difference if you insert the time taken to run the script.
 
Back
Top