Jump to content

|PHP| Interesting Scripts for your Website


Auntie Mangos
 Share

Recommended Posts

Yea I think so too, and that one is really bothering me because it's not pretty so I wrote this...

<?
require_once('config.php');

mysql_connect("$ip", "$user", "$pass");
mysql_select_db ("$mangoscharacters");
$race = array();
$class = array();
$query = "select race, class from characters";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
   $race[$row['race']]++;
   $class[$row['class']]++;
}

echo "Humans: $race[1]
";
echo "Dwarves: $race[3]
";
echo "Night Elf: $race[4]
";
echo "Gnome: $race[7]
";
echo "Draenei: $race[11]
";
echo "Orc: $race[2]
";
echo "Undead: $race[5]
";
echo "Tauren: $race[6]
";
echo "Troll: $race[8]
";
echo "Blood Elf: $race[10]

";
echo "Druid: $class[11]
";
echo "Hunter: $class[3]
";
echo "Mage: $class[8]
";
echo "Paladin: $class[2]
";
echo "Priest: $class[5]
";
echo "Rogue: $class[4]
";
echo "Shaman: $class[7]
";
echo "Warlock: $class[9]
";
echo "Warrior: $class[1]
";
echo "Death Knight: $class[6]
";
?>

I haven't tested it yet but it should be much more efficient.. please correct me if I'm wrong though

Might be faster in amount of queries, but count() is faster in general. Should be a way to have multiple count() statements in 1 query.

Link to comment
Share on other sites

  • Replies 171
  • Created
  • Last Reply

Top Posters In This Topic

  • 3 weeks later...

In th scripts * Top Arena i want see team order by range but it's doesn't work, i test this:

$sql = mysql_query("SELECT arena_team.arenateamid,arena_team.name,arena_team.captainguid,arena_team.type FROM arena_team ORDER BY arena_team_stats.rating DESC");

But i have this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homez.17/heroes/www/site/content/arene.php on line 81

and noting appen ...

How make this range by rating please ?

EDIT:

I found the good sql ^^

It's:

$sql = mysql_query("SELECT arena_team.arenateamid,arena_team.name,arena_team.captainguid,arena_team.type,arena_team_stats.rating FROM arena_team,arena_team_stats where arena_team.arenateamid = arena_team_stats.arenateamid ORDER BY arena_team_stats.rating DESC");

:)

Link to comment
Share on other sites

  • 3 weeks later...

I've rewritten the race/class count script, now it's both code-efficient and fast, for 0.12 just replace the "$n <= 11" with "$n <= 10" in the second loop and delete the Death Knight row. Enjoy :-)

<?
require_once('config.php');
mysql_connect("$ip", "$user", "$pass");
mysql_select_db ("$mangoscharacters");
$race = array();
$class = array();
for($n=1; $n <= 11; $n++){
   $sql = "SELECT COUNT(race) FROM `characters` WHERE `race`=".$n;
   $query = mysql_query($sql);
   $race[$n] = mysql_result($query, 0);
   mysql_free_result($query);
}
for($n=1; $n <= 11; $n++){
   $sql = "SELECT COUNT(class) FROM `characters` WHERE `class`=".$n;
   $query = mysql_query($sql);
   $class[$n] = mysql_result($query, 0);
   mysql_free_result($query);
}

echo "Humans: ".$race[1]."
";
echo "Dwarves: ".$race[3]."
";
echo "Night Elf: ".$race[4]."
";
echo "Gnome: ".$race[7]."
";
echo "Draenei: ".$race[11]."
";
echo "Orc: ".$race[2]."
";
echo "Undead: ".$race[5]."
";
echo "Tauren: ".$race[6]."
";
echo "Troll: ".$race[8]."
";
echo "Blood Elf: ".$race[10]."

";
echo "Druid: ".$class[11]."
";
echo "Hunter: ".$class[3]."
";
echo "Mage: ".$class[8]."
";
echo "Paladin: ".$class[2]."
";
echo "Priest: ".$class[5]."
";
echo "Rogue: ".$class[4]."
";
echo "Shaman: ".$class[7]."
";
echo "Warlock: ".$class[9]."
";
echo "Warrior: ".$class[1]."
";
echo "Death Knight: ".$class[6]."
";
?>

Link to comment
Share on other sites

Would be nice, if someone could re-do the registration script with a second check for password, like:

USERNAME
PASSWORD
PASSWORD2

and check if both passwords are the same. Because a lot of dudes type some typos in passwords and then create other accounts...

Link to comment
Share on other sites

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

Username <input type="text" name="username">

Password <input type="password" name="password">

Password <input type="password2" name="password2">

<input type="submit" name="registration">

</form>

$username = $_POST['username'];

$password =$_POST['password'];

$password2 =$_POST['password2'];

if ($username==NULL | $password==NULL | $password2==NULL{

echo "Some empty field.";

echo "Try again.";

}

else{

// ¿Same pass?

if ($password!=$password2) {

echo "Pass not match";

}

else

{

$qry_check_username = mysql_query("SELECT username FROM `account` WHERE username='$username'");

if (check_for_symbols($_POST[password]) == TRUE || check_for_symbols($username) == TRUE || mysql_num_rows($qry_check_username) != 0)

{

echo "Error with creating account, might already be in use or your username / password has invalid symbols in it.";

}

else

{

$password = sha_password($username,$_POST['password']);

mysql_query("INSERT INTO account (username,sha_pass_hashl) VALUES

('$username', '$password')");

echo "Account created.";

}

....

...

..

.

You can, if you want, also check email:

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

Username <input type="text" name="username">

Password <input type="password" name="password">

Password <input type="password2" name="password2">

E-mail : <input type="email" name="email">

<input type="submit" name="registration">

</form>

$username = $_POST['username'];

$password =$_POST['password'];

$password2 =$_POST['password2'];

$email = $_POST['email'];

if ($username==NULL | $password==NULL | $password2==NULL | $email==NULL){

echo "Some empty field.";

echo "Try again.";

}

else{

// ¿Same pass?

if ($password!=$password2) {

echo "Pass not match";

}

else

{

$qry_check_username = mysql_query("SELECT username FROM `account` WHERE username='$username'");

if (check_for_symbols($_POST[password]) == TRUE || check_for_symbols($username) == TRUE || mysql_num_rows($qry_check_username) != 0)

{

echo "Error with creating account, might already be in use or your username / password has invalid symbols in it.";

}

else

{

$password = sha_password($username,$_POST['password']);

mysql_query("INSERT INTO account (username,sha_pass_hash,email) VALUES

('$username', '$password', '$email)");

echo "Account created.";

}

Link to comment
Share on other sites

Hmm, another script would be awesome, I had it somewhere, but I think its gone after reformating... Top 5 or Top 10. Sorta script that will display character names (except Admin/GM/Mod) using the formula: EXP+LEVEL+HONOR+LIFETIMEKILLS+GOLD. Would be really handy for websites that wish to display more info than usual.

Link to comment
Share on other sites

I am curious. Is it possible to make a web display for the number of users currently queued for battlegrounds/arena? Sort of like "Warsong Gulch - A: 5/10 H: 7/10"

If I knew where the current queue count was stored, I might be able to cobble something together, but I have no clue, so I'm not even sure it's possible.

Link to comment
Share on other sites

now it's both code-efficient and fast

No, it is very much not. Never use SQL queries within a for loop if you can avoid it - you are now using no less than 22 queries just to get total char and race counts. You can obtain the same result with a single query for each type:

$query = "SELECT     `race`,
                     COUNT(`race`)
         FROM       `characters`
         GROUP BY   `race`";

If I knew where the current queue count was stored, I might be able to cobble something together, but I have no clue, so I'm not even sure it's possible.

I am not entirely sure, but I don't believe the queue's are stored anywhere - there isn't much point really. If there was a GM command to show them you might be able to use the SOAP interface to obtain these fields, but I can't find any command that would let you do that.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

I wane use some scripts in joomla but is it posble to make them so that when you register for joomla you make an game accound as well. Im not in to php other than changing scripts on the hand of example lines and put in b4 or after things.

So if it can be done i would like to know if it can be done like i want it above and if so how.

Would be great if somone knows how to make an joomla extension with all the scripts :P

Link to comment
Share on other sites

Guys, please use mysql_close on files, this can lead to un-needed mysql server load with un-closed connections..

Any decent webserver will maintain a connection pool of opened MySQL connections anyway and if it does not PHP will close the connection at termination, so it matters very little. In fact, opening and closing connections continously costs more resources than reusing an existing connection - for precisely this reason does PHP also support persistent connection methods (mysql_pconnect) and does Apache keep a connection pool in the first place (if properly configured for high-load situations anyway).

If you really, really have to limit the number of connections to your database (which is very unlikely as Mangos only uses 3 and PHP/Apache will usually create less than a dozen for a heavy load website) you can use the mysql.max_persistent directive in your PHP.ini. Again, you really should never have to.

As I cannot stress this enough: unclosed connections are generally a good thing - they decrease CPU load & latency, not increase it! MySQL is these days set to 151 maximum connections by default (documentation) and can generally handle 500 ~ 1000 open connections with no problems. I very much doubt you'll see more than ~ 20 or so connections in a normal mangos + website scenario.

Just about the only reason you should ever want to use mysql_close is if you are running your database on a shared hosting account limited to 1 or 2 connections - but I very much doubt you could or want to do that with your Mangos database.

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

can any one help me with this script? im very noobie at php i find this script but i get a double number example 11 00 etc. what im trying is to get the horde and ally players online

    <?
require_once ( 'config.php');
$conn = mysql_connect($host, $user, $pass) or die('Connection failed: ' . mysql_error());

mysql_select_db($mangoscharacters, $conn) or die('Select DB failed: ' . mysql_error());

$query = mysql_query("SELECT guid FROM characters WHERE race IN (1, 3, 4, 7, 11) AND online='1'", $conn);
       $total = mysql_num_rows($query);
       echo $total;

echo $total; 
?>

Link to comment
Share on other sites

That query only gets number of aly online. You need another one for horde. And you have double echo $total.

<?
require_once ( 'config.php');
$conn = mysql_connect($host, $user, $pass) or die('Connection failed: ' . mysql_error());

mysql_select_db($mangoscharacters, $conn) or die('Select DB failed: ' . mysql_error());

$query = mysql_query('SELECT race FROM characters WHERE online = 1 ');
$horde = 0; $aly = 0;
while($data = mysql_fetch_array($query))
{
   if(in_array($data['race'], array(1, 3, 4, 7, 11)))
       $aly ++;
   else
       $horde ++;
}
echo 'Horde Online: '.$horde.' Aliance Online: '.$aly;
?>

Link to comment
Share on other sites

Small improvements to the account registration script:

<?php
$realmd = array(
'db_host'=> 'localhost',        // Host IP
'db_username' => 'root',        // Database login-name
'db_password' => 'mangos',      // Database login-pass
'db_name'=> 'realmd',           // Database name
);

function check_for_symbols($string)
{
   $len=strlen($string);
   $allowed_chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   for($i=0;$i<$len;$i++)if(!strstr($allowed_chars,$string[$i]))
       return TRUE;
   return FALSE;
}

function sha_password($user,$pass)
{
   $user = strtoupper($user);
   $pass = strtoupper($pass);
   return SHA1($user.':'.$pass);
}

$realmd_bc_new_connect = mysql_connect($realmd[db_host],$realmd[db_username],$realmd[db_password]);
if ($realmd_bc_new_connect)
   $selectdb = mysql_select_db($realmd[db_name],$realmd_bc_new_connect);
else
{
   echo "Could NOT connect to db: Configs (Name/Pass/Port/IP) are incorrect";
   die;
}

if ($realmd_bc_new_connect && !$selectdb)
{
   echo "Could NOT connect to db: Database does not exist!";
   die; 
}

if ($_POST['registration'])
{
   $username = $_POST['username'];
   $password = sha_password($username,$_POST['password']);
   $qry_check_username = mysql_query("SELECT username FROM `account` WHERE username='$username'");

   if (check_for_symbols($_POST[password]) == TRUE)
   {
       echo "Error with creating account: password has invalid symbols in it.";
   }
   else if (check_for_symbols($username) == TRUE)
   {
       echo "Error with creating account: username has invalid symbols in it.";
   }
   else if (mysql_num_rows($qry_check_username) != 0)
   {
       echo "Error with creating account: name is already in use.";
   }
   else
   {
       mysql_query("INSERT INTO account (username,sha_pass_hash) VALUES
('$username','$password')");
       echo "Account created.";
   }
}
else
{
   ?>
   <html>
   <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
   Username <input type="text" name="username">
   Password <input type="password" name="password">
   <input type="submit" name="registration">
   </form>
   </html>
   <?php
}
?>

The error messages are now split into pieces so you can actually tell what is wrong (instead of being given 3 options like "The name is already in use OR the pass contains incorrect symbols OR the name contains incorrect symbols)

and for as far as i know, numbers are allowed in the name and pass as well so i've added them to the allowed_chars variable

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use