954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need code for something like example.com/key?=23j32hf3j1

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

So what do you need?

MagicMedia
Junior Poster
149 posts since Jul 2010
Reputation Points: 20
Solved Threads: 35
 

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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).

minitauros
Junior Poster
109 posts since Apr 2011
Reputation Points: 36
Solved Threads: 17
 

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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>
IIM
Junior Poster
165 posts since Jun 2011
Reputation Points: 46
Solved Threads: 37
 

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.

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 
$sql = 'INSERT INTO table (key)
     VALUES ("'.$key.'")';
$result = mysql_query($sql);
$sql = 'SELECT key
     FROM table';
$result = mysql_query($sql);
MagicMedia
Junior Poster
149 posts since Jul 2010
Reputation Points: 20
Solved Threads: 35
 

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");
rv1990
Junior Poster in Training
51 posts since Aug 2011
Reputation Points: 17
Solved Threads: 7
 

So what would:

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

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

And how about storing the code in a text file?

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

_8Bit
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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

Ichcha
Light Poster
46 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: