bubblybabs Posted July 12, 2007 Share Posted July 12, 2007 I am looking to create a random number and then combining that with time()... The coding I have was achieved by using several types of coding I found on the net and then playing around with them in an attempt to make the random number truly random (I understand that some coding isn't always random even though it's supposed to be) therefore I'm sure this is sloppy coding to those of you who have been doing this a long time... Here it is: <?php /** * Generate and return a random string * * The default string returned is 8 alphanumeric characters. * * The type of string returned can be changed with the "seeds" parameter. * Four types are - by default - available: alpha, numeric, alphanum and hexidec. * * If the "seeds" parameter does not match one of the above, then the string * supplied is used. * * @author Aidan Lister <aidan@php.net> * modified a tad bit by BubblyBabs by randomzing the length of the string (see line below) * example which produces random length string 11-19 characters long: str_rand(rand(11,19), 'numeric'); * to view put: echo str_rand(rand(11,19), 'numeric'); * @version 2.1.0 * @link http://aidanlister.com/repos/v/function.str_rand.php * @param int $length Length of string to be generated * @param string $seeds Seeds string should be generated from */ function str_rand($length = 8, $seeds = 'alphanum') { // Possible seeds $seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $seedings['numeric'] = '09182736454637281959513074682987456321085021479630946132587'; $seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $seedings['hexidec'] = '0123456789abcdef'; // Choose seed if (isset($seedings[$seeds])) { $seeds = $seedings[$seeds]; } // Seed generator list($usec, $sec) = explode(' ', microtime()); $seed = (float) $sec + ((float) $usec * 100000); mt_srand($seed); // Generate $str = ''; $seeds_count = strlen($seeds); for ($i = 0; $length > $i; $i++) { $str .= $seeds{mt_rand(0, $seeds_count - 1)}; } return $str; } $acceptedChars = '09182736454637281959513074682987456321085021479630946132587'; $max = strlen($acceptedChars)-1; $code1 = null; for($i=0; $i < rand(3,; $i++) { $code1 .= $acceptedChars{mt_rand(0, $max)}; } $ttcode1 = $code1 . $from_remoteport . time(); $ttcode2 = str_rand(rand(5,, 'numeric'); $randnum = substr($ttcode2,0,10); ?> I then add this to my ecard program to create a cardid that combines time and the random number (the coding above is called by the page that sends the ecard emails out): $cardid = time() . $randnum; All seems to work fine... HOWEVER, the ecard program sends an email out with the proper cardid but what is saved to the MySQL table is ALWAYS this cardid: 2147483647 Looking on the net this number (2147483647) seems to be an error code? My question, what is wrong with this coding and how do I achieve my goal and what does this error code mean? I should add that I am trying to make it so that the cardid isn't ALWAYS 11 or 15 or 19 digits long... I want it to be variable... I have a working portion of the code here: http://ecards.fractalfairy.com/randomttcode.php but it won't work in my ecard coding... Thanks, Babs Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 Oooo! I think I figured it out... Had to change the MySQL column from int to bigint... :-) Babs Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 Edit: Dumb remark thought it was for a timestamp, my bad =) Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 I'm almost afraid to think of what your comment was frost110, hope it wasn't too mean... Anyway, I've a new problem... I can now save cards after changing to bigint... However when I try to view the eCard I get an error: Parse error: syntax error, unexpected T_VARIABLE in /home/ecards/public_html/viewcard.php on line 30 Line 30 is this: $cardid = (bigint)$_GET['cardid']; It used to be $cardid = (int)$_GET['cardid']; but once I changed the MySQL table from int to bigint I thought I had to change this line as well... I get that darn error number again (2147483647) instead of the ecard id# if I leave it as (int)... Looking on the net it seems I should have a missing terminating semi-colon but I can't find one... Here is the coding above line 30: session_start(); include('inc/adodb/adodb.inc.php'); # load code common to ADOdb include_once('config.php'); include('inc/UIfunctions.php'); $page = new pagebuilder; include_once('inc/setLang.php'); $page->langargs = "&cardid=$cardid"; $page->showHeader(); So, shouldn't (int) be changed to (bigint)? Thanks, Babs Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 No int shouldn't be changed to big int. My remark was not mean, it was a suggestion which after reading through the post correctly was unnecessary and irrelevant. Anyhow you may need the number to be a float or double, but (int) should be fine via the php way. Keep the same as the (int) for the php but leave it as bigint in the MySQL portion see if it works. Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 Thanks frost110... I tried just the (int) and it would not work, I kept getting that error code... However, what I did do was make a function to call upon the cardid number and check to make sure it wasn't too long and only had numbers in it (using the same strategy I used when trying to correct some coding for the newsid a few days ago)... I change the coding from: $cardid = (int)$_GET['cardid']; to: // Check $cardid for numbers only // If anything else is put into the address bar an error message will show if (!validCardid($cardid)) { echo '<span class="error">'.$getcard01.'</span>'; $page->showFooter(); exit; } // Check size of cardid and throw errors if it is longer than 30 characters if (strlen($cardid) > 30) { echo '<span class="error">'.$getcard01.'</span>'; $page->showFooter(); exit; } if (!$cardid) { echo '<span class="error">'.$getcard01.'</span>'; $page->showFooter(); exit; } and now it's working... I had to add a function on another page called to this one but now it works... Babs Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 Right on. You do know that you did not have to cast that get data to an int right? You could of inserted it into the table without doing that part. Just an FYI, I bet if you just left the (int) part out it would of been fine, to check if it was indeed all numeric you could of just used www.php.net/is_numeric Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 Right on. You do know that you did not have to cast that get data to an int right? You could of inserted it into the table without doing that part. No, I didn't know that... Doesn't the int tell the coding you only want an integer? That is what I was thinking it meant and that is what I want... Is this correct? I should elaborate, at first I thought that but then I kept getting an error so then I thought perhaps it was indicating the MySQL table... Just an FYI, I bet if you just left the (int) part out it would of been fine, to check if it was indeed all numeric you could of just used www.php.net/is_numeric Looking at is_numberic there seems to be a chance that letters could be considered acceptable and some of the coding used on that page seems longer than what I used... I wanted to make sure that only numbers are given the OK to use the coding... Thank-you for taking the time to help me figure this out... Babs Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 Right on. You do know that you did not have to cast that get data to an int right? You could of inserted it into the table without doing that part. Just an FYI, I bet if you just left the (int) part out it would of been fine, to check if it was indeed all numeric you could of just used www.php.net/is_numeric Looking at is_numberic there seems to be a chance that letters could be considered acceptable and some of the coding used on that page seems longer than what I used... I wanted to make sure that only numbers are given the OK to use the coding... Thank-you for taking the time to help me figure this out... Babs No, I didn't know that... Doesn't the int tell the coding you only want an integer? That is what I was thinking it meant and that is what I want... Is this correct? It does, but it is not necessary in PHP. PHP is a very lieniant language. It is not a necessary step. Looking at is_numberic there seems to be a chance that letters could be considered acceptable and some of the coding used on that page seems longer than what I used... I wanted to make sure that only numbers are given the OK to use the coding... Probably right, thats why they invented regular expressions! www.php.net/ereg if (ereg('([0-9]*)', $number)) { echo 'It is only a number!'; } =) And np. Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 Yes, the coding I have to check and make sure there are only numbers is: function validCardid($cardid) { if (eregi("^([0-9])+$",$cardid)) return true; else return false; } Babs Quote Link to comment Share on other sites More sharing options...
corbin Posted July 12, 2007 Share Posted July 12, 2007 This is kind of random, and I'm sorry if it's off topic. If you make PHP cast the int type on a string, it usually just makes it 0 so sometimes type casting can make stuff go crazy if for some reason you get text. Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 12, 2007 Author Share Posted July 12, 2007 This is kind of random, and I'm sorry if it's off topic. If you make PHP cast the int type on a string, it usually just makes it 0 so sometimes type casting can make stuff go crazy if for some reason you get text. Corbin, I am so sorry but I have no idea what you mean... Is my coding incorrect or can it cause a problem? Babs Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 13, 2007 Author Share Posted July 13, 2007 I guess I should technically call this post solved... Babs Quote Link to comment Share on other sites More sharing options...
per1os Posted July 13, 2007 Share Posted July 13, 2007 This is kind of random, and I'm sorry if it's off topic. If you make PHP cast the int type on a string, it usually just makes it 0 so sometimes type casting can make stuff go crazy if for some reason you get text. Corbin, I am so sorry but I have no idea what you mean... Is my coding incorrect or can it cause a problem? Babs He is basically saying don't use the (int) casting method on strings as sometimes it causes the data to go haywire. Your code is good as long as it works for you. Quote Link to comment Share on other sites More sharing options...
bubblybabs Posted July 13, 2007 Author Share Posted July 13, 2007 Oh, OK... The int was part of the original code... It's been deleted with my modification so I guess no worries... Ah, so much to learn, so much... Babs Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.