6
Contributors
14
Replies
15
Views
6 Years
Discussion Span
Last Post by Ichcha
0

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

0
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).

0

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

Edited by _8Bit: n/a

0

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>
0

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.

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

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");
0

So what would:

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

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

0

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

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.