Inserting Scores Into The Database
Inserting scores is very simple to do. We will simply make a GET request from our iPhone app to our put_score.php page and pass in information through the GET parameters. An example of this might be
http://icodeblog.com/ws/put_score.php?udid=0123456789012345678901234567890123456789&name=brandontreb&score=210.13&secret=some_secret
Here is an explanation of the variables.
Variable
Description
secret |
This is some password that only you know. It will prevent people from inserting invalid data into your database. We will hardcode this into the script below |
udid |
This is the UDID of the user’s device. It will be used to uniquely identify each user |
name |
The name to display in the leadboard |
score |
The score for that given user. |
And now the code for put_score.php
<?php
// put_score.php
/** MySQL database name */
define('DB_NAME', '');
/** MySQL database username */
define('DB_USER', '');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', $_ENV{DATABASE_SERVER});
$table = "highscores";
// Initialization
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);
// Error checking
if(!$conn) {
die('Could not connect ' . mysql_error());
}
if($_GET['secret'] != "some_secret") {
die('Nothing to see here...');
}
// Localize the GET variables
$udid = isset($_GET['udid']) ? $_GET['udid'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$score = isset($_GET['score']) ? $_GET['score'] : "0.00";
// Protect against sql injections
$udid = mysql_real_escape_string($udid);
$name = mysql_real_escape_string($name);
$score = mysql_real_escape_string($score);
// Insert the score
$retval = mysql_query("INSERT INTO $table(
udid,
name,
score
) VALUES (
'$udid',
'$name',
'$score'
)",$conn);
if($retval) {
echo "Inserted score $score for $name";
} else {
echo "Unable to insert score " . mysql_error();
}
mysql_close($conn);
?>
So we see a lot of the same initialization code as we did in our create_db.php method. As you can see, there is not a lot of code here. We first just localize the GET variables and escape them to ensure that they can’t be sql injected. PHP developers are so lazy that they always fail to do this. It is one line of code that can prevent a huge security flaw.
After localization and sanitation, we simply insert these values into the database and print out the result. The last part of our server code involves displaying the leaderboard. Now, we could write a service for returning xml and display it natively in the application, however displaying a table inside of a webview is much simpler.
So, we are going to output this data into an HTML table that will get displayed inside of a UIWebView. Keep in mind that my table looks like crap and you should definitely style it before using it in your applications.
分享到:
相关推荐
这篇“iPhone Coding Tutorial – In Application Emailing”教程将指导开发者如何在iPhone应用程序中集成电子邮件功能,无需离开应用就能发送邮件。这篇博客文章的链接虽然不可用(已替换为示例文本),但我们可以...
这篇教程“iPhone Coding Tutorial – Inserting A UITextField In A UIAlertViewController”将会教你如何在现代iOS环境中,在警告视图控制器中添加一个文本字段。 首先,我们需要了解`UIAlertController`的基本...
online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单表表单模板上传使用说明online coding单...
A visual step-by-step guide to writing code in Python. Beginners and experienced ...With coding theory interwoven into the instructions for building each game, learning coding is made effortless and fun.
An introduction to coding for constrained systems, Draft Version, 2001. by B H Marcus, R M Roth, P H Siegel
### 一、里德-所罗门编码(Reed-Solomon Coding)基础 里德-所罗门编码是一种非二进制线性错误校正码,广泛应用于数据存储和通信系统中,以提供强大的错误检测和纠正能力。它由Irving S. Reed和Gustave Solomon于...
After reading this book you will be able to develop your own 2d and 3d videogames and use it on your presentations, to speed up your level design deliveries, test your game design ideas, work on your ...
百度的余凯老师在CVPR2012上的Tutorial
### JEECG Online Coding开发手册 v3.6 知识点总结 #### 1. 单表智能表单 [Online 表单开发] ##### 1.1 创建数据表单(界面如下图) - **创建单表**: 在创建单表时需要注意的是必须包含一个主键字段,并且该主键...
Beginning iPhone Games Development (Mar 2010) Part 2 of 2 Part 1 -> http://download.csdn.net/source/2283635 源代码 -> http://apress.com/book/view/9781430225997 iPhone games are hot! Just look at ...
《Coding Games in Scratch》是一本面向初学者的编程教材,主要使用Scratch编程语言来教授游戏开发的基础知识。Scratch是由麻省理工学院(MIT)的“终身幼儿园团队”开发的一款图形化编程工具,旨在帮助孩子们学习编程...
Beginning iPhone Games Development Apress, 2010年出品 You’ve probably already read and mastered Beginning iPhone 3 Development; Exploring the iPhone SDK, the best-selling, the second edition of ...
cvpr12_tutorial_sparse_coding.ppt
JEECG Online Coding开发手册v3.6主要介绍了如何使用JEECG平台进行Online表单的开发和配置。 知识点一:单表智能表单开发 在单表智能表单开发中,首先需要创建数据表单,必须为数据表单设置主键,并确保主键为ID,...
How to give your users access to their iPhone libraries from within games The tools and techniques of 3D audio for creating even more realistic gaming experiences How to do networking right, including...
Beginning iPhone Games Development (Mar 2010) Part 1 of 2 Part 2 -> http://download.csdn.net/source/2283657 源代码 -> http://apress.com/book/view/9781430225997 iPhone games are hot! Just look at ...
Using an example as a tutorial, we will relate the driving principles and you'll see how you can implement these principles to develop any games on the platform. We begin by setting expectations and ...