I am unsure who made this code first but i made it 562 rs2hd from 317
Purpose: Gives people GP/items for voting on a website.
Difficulty: 5 (It just uses MySQL, that doesn't make it rise to 10...)
[size=18pt]WEBSITE SIDE[/size]
-> A webserver/hosting account with MySQL working.
-> COM SQL driver (
Downloading - com.zip @ UppIT)
-> A website obviously
In your web hosting make a new PHP file and call it vote.php, add the following in to it:
Code:
<?php
$host="localhost";
$username="YOUR MYSQL USERNAME";
$password="YOUR MYSQL PASSWORD";
$db_name="DATABASE NAME";
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username=$_POST['username'];
$voteid = ($_POST['votenum']);
$username = stripslashes($username);
$username = mysql_real_escape_string($username);
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
if(clean_requests($time))
{
if(vote_entries($ip) == 0)
{
mysql_query("INSERT INTO Votes (username, ip, time, recieved) VALUES ('$username', '$ip', '$time', '0')", $link) or die("An internal error has occured please try again later.<br/>To go back click <a href='index.php'><b>HERE</b></a>");
header("Location: ".votenow($voteid)."");
}
else
{
die("You have already voted once today<br/>To go back click <a href='index.php'><b>HERE</b></a>");
}
}
else
{
die("An internal error has occured please try again later.<br/>To go back click <a href='index.php'><b>HERE</b></a>");
}
function clean_requests($time)
{
$query = mysql_query ( "SELECT * FROM Votes");
$i = 0;
while($row = mysql_fetch_array($query))
{
$i++;
$timerequested = $row['time'];
if($time-$timerequested > 12*3600)
{
if (!mysql_query( "DELETE FROM Votes WHERE time='$timerequested'"))
{
return false;
}
}
}
return true;
}
function vote_entries($ip)
{ $entries = mysql_query ( "SELECT * FROM Votes WHERE ip = '$ip'" );
if ( !$entries ) {
die ( "Unable to get number of entries: " . mysql_error () );
}
return mysql_num_rows ( $entries );
}
function votenow($votenum)
{
$url = "";
if($votenum)
{
switch($votenum)
{
case 1:
$url = "VOTE LINK 1;
break;
case 2:
$url = "VOTE LINK 2";
break;
case 3:
$url = "VOTE LINK 3";
break;
default: die("Error!"); break;
}
}
return $url;
}
?>
The above code has three voting websites configured to work with it. If you need more, just ask me, it's easy to add a few more. Just a copy paste of code required.
You will need to replace the following variables with your MySQL Details, if you don't have them, ask your host:
YOUR MYSQL USERNAME
YOUR MYSQL PASSWORD
DATABASE NAME
If you haven't already create a new database and call it whatever you like. I called mine vote. Your MySQL details may look like this:
YOUR MYSQL USERNAME: ammaris1_vote
YOUR MYSQL PASSWORD: 738883hdgry7
DATABASE NAME: vote
Now, time to explain: The above PHP code connects to the database and when you vote, it inserts the username, time and a given variable of 0 when you vote the first time.
Now, open PhpMyAdmin you probably have it, if you don't ask your host to configure it for you. In PhpMyAdmin, open the newly created database and run this SQL query on it:
Code:
CREATE TABLE IF NOT EXISTS `votes` (
`username` char(50) DEFAULT NULL,
`ip` char(50) DEFAULT NULL,
`time` int(50) DEFAULT NULL,
`recieved` int(1) DEFAULT NULL
)
This query creates the tables that are needed to insert the information.
Now go back to your file manager and create a HTML file, this time call it vote.html. Add this code into it:
Code:
<form name="form" method="post" action="vote.php">
Username: <input name="username" type="text" id="username">
Vote # <select name="votenum">
<option value="1">VOTE WEBSITE 1</option>
<option value="2">VOTE WEBSITE 2</option>
<option value="3">VOTE WEBSITE 3</option>
</select><br/><br/>
<input type="submit" value="Vote!"/>
</form>
This code connects to vote.php and this is where your players enter their username and where they want to vote. Replace VOTE WEBSITE 1, VOTE WEBSITE 2 and VOTE WEBSITE 3 with the places they are voting, if you don't want to use all 3 spaces just remove the whole line of code where VOTE WEBSITE # is mentioned.
-----------------------------------------------------------------------------------------------------------
[size=18pt]Server Side[/size]
This is the part that most people get wrong. Add your COM driver which you downloaded into your server files and then make a new Java file called MYSQL.java (With capitals). Add this into it:
Code:
package com.rs2hd.packethandler;
import java.sql.*;
import java.security.MessageDigest;
public class MYSQL {
public static Connection con = null;
public static Statement stm;
public static void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/database", "username", "password");
stm = con.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
ResultSet rs = stm.executeQuery(s);
return rs;
} else {
stm.executeUpdate(s);
}
return null;
} catch (Exception e) {
System.out.println("MySQL Error:"+s);
e.printStackTrace();
}
return null;
}
public static void destroyCon() {
try {
stm.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean checkVotes(String playerName)
{
try {
Statement statement = con.createStatement();
String query = "SELECT * FROM Votes WHERE username = '" + playerName + "'";
ResultSet results = statement.executeQuery(query);
while(results.next()) {
int recieved = results.getInt("recieved");
if(recieved == 0)
{
return true;
}
}
} catch(SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean voteGiven(String playerName)
{
try
{
query("UPDATE Votes SET recieved = 1 WHERE username = '" + playerName + "'");
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
Search for this code: "jdbc:mysql://127.0.0.1/database", "username", "password"
Replace database with your database name and username and password with your MySQL details.
I added this next part to player.java
Add the import for MYSQL
Search
Under that add this
Code:
public void VoteCheck() {
if(MYSQL.checkVotes(this.details.getUsername())){
if(MYSQL.voteGiven(this.details.getUsername()))
{
getInventory().addItem(995,200000);
getActionSender().sendMessage("Thank you for voting!");
}
}
}
Above that code add
Code:
public void extraAutoSave() {
MYSQL.createConnection();
}
Now goto commandspackethandler.java and the command
Code:
if (cmd[0].startsWith("::vote")) {
player.VoteCheck();
}
then go to autosaveevent.java and find
Code:
.savePlayer(player);
under it add
Code:
player.extraAutoSave();
Now save and compile, you should not get any errors, post any here and I'll try to help.
Post any problems/comments below!
This should work. I made mine a little different cause i am using highscores too.
Credits:Whoever made vote4cash first 85%(they made it)
me 15%(I converted it)