Hi!
I'm new here, I have been stuck on this for about to weeks. I am trying to replicate something like the humble bundle's website, where they give you a url like http://humblebundle.com/?key=hj2qg4j2h34234 and you can access the page. I have tried for about 2 weeks but can't figure it out, I know it uses mysql, has anyone seen a script for something like this? Or can anyone give me some code that does this please?
Thanks,
_8Bit

So what do you need?

I need a piece of code that will let me add a randomly generated number to a database, then another piece of code, that checks if the code is in the database, if it is it will show the page, if not, it will output Invalid Code or something?
Eg.
Valid Code (http://example.com/?key=hsw1oi3u23) outputs the website.
No Code (http://example.com/) outputs a main site, where you can get a code.
Valid Code (http://example.com/?key=fswiwdwdu23) outputs Invalid Code!.

Like http://humblebundle.com/ for example

function randomHash($length)
{
    if (!$length)
        $length = 6;

    $array = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));

    for($i=0; $i<$length; $i++)
    {
        $random = mt_rand(0, count($array)-1);
        $random_hash .= $array[$random];
    }

    return $random_hash;
}

This creates a random hash with the length of $length.

Add it to your database with a simple insert query (INSERT INTO...) and then check if it is present with a simple select query (SELECT ... FROM).

Thanks, I am really new to mysql, could you please give me the code for that as well?
Thanks :P

There are different ways to do it...
one of these are:-
1. Use a database and in that map it according to the value ,then convert it using some encrption like md5,sha1(Manual about md5 and then retrieve it and use a variable "id" and store in it.
Then write this..

<a href="example.php?key='".$id."'>"

2. Use form and submit data:-

<form method="POST" action="example.php">
Enter a key<input type="text" name="key">
</form>

Thanks, I know how to do that but I have no idea how to add the value to the database and then check if it is in the database.

$sql = 'INSERT INTO table (key)
     VALUES ("'.$key.'")';
$result = mysql_query($sql);
$sql = 'SELECT key
     FROM table';
$result = mysql_query($sql);

You need to show your mysql table . you might be new to the MySql.
have to use query like:

mysql_query("insert into tablename values (values of the attributes)") or die("error");

So what would:

$sql = 'SELECT key
     FROM table';
$result = mysql_query($sql);

output if the key existed? and if it didn't exist?

And how about storing the code in a text file?

I'm thinking about using a flat file now, any idea's on that?

can't figure it out with mysql so i used flat files storage

Bump, I would like to know how to do it with mysql

So what would:

$sql = 'SELECT key
     FROM table';
$result = mysql_query($sql);

output if the key existed? and if it didn't exist?

mysql_query returns resource on success and false on error. see this for more understanding.http://php.net/manual/en/function.mysql-query.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.