1

I need help to auto generate a random unique permanent secure ID based on current url, store it automatically inside database and display it in php page ?

I have a multisite and everytime someone create a new subdomain I like to display on the page a unique secure id based on current new subdomain and display it in his page like :

ID PAGE SECURE NUMBER: 6465465DSGDGDGDG4564EEEZ7778

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51

2 Answers2

1

PHP has a hashing function you could use. I would also add a random salt to it, can be anything really like a string of text or numbers

$url = $_SERVER['HTTP_HOST']; 
$salt = "i.am.a.rand0m.str1ng.Of.ChArAcT3r$";
echo hash('sha1', $url . $salt);

There are other hashes you could use: http://www.php.net/manual/en/function.hash.php

There is also the password_hash function of PHP, but this requires 5.5.0 or greater.

edit swapped out the URL for $_SERVER['HTTP_HOST'], though there are probably better ways of getting the current URL.

stckrboy
  • 379
  • 10
  • 16
  • I need help to auto generate a random unique permanent secure ID based on current url not a fixed url – user3318822 Feb 17 '14 at 11:55
  • Google was just an example, you can substitute that using something like $_SERVER['HTTP_HOST']. I'll update my example with that – stckrboy Feb 17 '14 at 11:58
  • I just did it but the number don't change when I change url – user3318822 Feb 17 '14 at 12:05
  • So what you suggest ? for the moment your solution not working – user3318822 Feb 17 '14 at 12:15
  • Ah http_host may not be picking up the subdomain and treating it just as your main domain. I think you'll need to find a way to grab the sudomain URL, maybe [one of these](http://stackoverflow.com/questions/5292937/php-function-to-get-the-subdomain-of-a-url) solutions can help? – stckrboy Feb 17 '14 at 12:17
  • I find a a way to grab subfolder and it work. Now how to store it in database? – user3318822 Feb 17 '14 at 12:35
  • Do you have a database set up where you can store the details? What have you tried so far with inserting the data? If you haven't already looked, read up on using [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). There are loads of posts on stack (and around the net) for inserting data. Have a search for PDO PHP MySQL. I would adivse against using mysql_* queries as they have been deprecated now. Some more info to read over - http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496 – stckrboy Feb 17 '14 at 13:23
0

create an table tab1 with pagename,randomnum

<?php
$myurl = basename($_SERVER['PHP_SELF']);

$q1=mysql_query("select * from tab1 where pagename='$myurl' ");
$c=mysql_num_rows();
$row=mysql_fetch_array($q1);
if($c>0)
{
 $url=$_SERVER['REQUEST_URI']."?secureid=$row['randomnum']";
    header( "Location: $url" );
}
else
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand = substr( str_shuffle( $chars ), 0,8 );
$q2=mysql_query("insert into tab1 (pagename,randomnum) values ('$myurl','$rand')");
}
?>
Steve Bals
  • 1,949
  • 6
  • 22
  • 32
  • 1
    mysql_ is deprecated. do not use/recommend it – user2907171 Feb 17 '14 at 11:42
  • Hi, what do you mean by "create an table tab1 with pagename,randomnum" ? I just cut and past the two code above in the same php page and it's not working.. – user3318822 Feb 17 '14 at 11:47
  • Dreamweaver said there is an error here: $url=$_SERVER['REQUEST_URI']."?secureid=$row['randomnum']"; – user3318822 Feb 17 '14 at 11:54
  • I paste this on the top: 0) { $url=$_SERVER['REQUEST_URI']."?secureid=$row['randomnum']"; header( "Location: $url" ); } ?> – user3318822 Feb 17 '14 at 11:56
  • and this in the middle: 0) { } else { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $rand = substr( str_shuffle( $chars ), 0,8 ); $q2=mysql_query("insert into tab1 (pagename,randomnum) values ('$myurl','$rand')"); } ?> – user3318822 Feb 17 '14 at 11:57
  • So what you suggest ? for the moment your solution not working – user3318822 Feb 17 '14 at 12:14