-8

I'm new to PHP. I want to write a simple PHP code that generates a random alphanumeric string as output based on query string parameters passed through the URL (HTTP GET method).

The parameters would be:

userid=

transactionid=

Example URL: http://testurl.org/file.php?userid=123&transactionid=4567

If the URL does not have the values mentioned above, the output would be ERROR

I have the following code which generates random strings:

<?php echo  rtrim(base64_encode(md5(microtime())),"=");?>

But they are not based on URL parameters.


I'm entering now the code below but is giving me a parse error. Do you see something missing below?

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
    <?php 
       function randomString() {
          return rtrim(base64_encode(md5(microtime())),"=");
       }
       echo (isset($_GET['userid'] && $_GET['userid']>"") ? randomString() : "ERROR";
    ?> 
 </body>

Code suggested by Fred-ii- and devJunk works. This is the final code that works based on their suggestions:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
<?php 
function randomString() {
    return rtrim(base64_encode(md5(microtime())),"=");
}

echo isset($_GET['userid']) && !empty($_GET['userid']) && isset($_GET['transactionid']) && !empty($_GET['transactionid']) ? randomString() : "ERROR";
?> 
 </body>
</html>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
adrian.m123
  • 85
  • 1
  • 3
  • 11
  • 1
    Stack Overflow is *not* a code writing service. You are expected to *try* to write code on your own and *then* seek assistance with any issues you have, – John Conde May 16 '15 at 00:49
  • I don't see the point in having a `random` string based on something else. random is random. It's lile "I want a car of any color - but it should be basically blue" – Jeff May 16 '15 at 01:00
  • @Jeff: that's correct. The purpose would be to generate the random string only if the mentioned parameters with a value are present in the URL. When not than ERROR – adrian.m123 May 16 '15 at 01:03
  • then just do a test before if there a values available. if not return error. like `echo isset($userid) ? rtrim(base.....))) : "ERROR"` – Jeff May 16 '15 at 01:05
  • possible duplicate of [PHP random string generator](http://stackoverflow.com/questions/4356289/php-random-string-generator) – Amit Verma May 16 '15 at 02:15
  • @adrian.m123 Reload my answer below under **Edit**. – Funk Forty Niner May 16 '15 at 03:44

3 Answers3

1

Check for the existence and content of both parameters:

if (isset($_GET['userid']) && !empty($_GET['userid']) && isset($_GET['transactionid']) && !empty($_GET['transactionid'])) {
    # generate random string
}

As someone said above, random is random, so passing values into the string is not of much use unless you're trying to salt your string.

vcanales
  • 1,818
  • 16
  • 20
1

Your best bet is to use multiple GET arrays and conditional statements, and "echo" the function if it meets both conditions.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

function randomString() {
   return rtrim(base64_encode(md5(microtime())),"=");
}

if (
    isset($_GET['userid']) 
    && 
    !empty($_GET['userid']) 
    && 
    isset($_GET['transactionid']) 
    && 
    !empty($_GET['transactionid'])
    ) 

{
   echo randomString();
}

else{
   echo "One GET array is not set or is empty.";
}

Nota:

  • If this code gives you a parse error, then you are using it with something else that is causing it.
  • This code was pre-tested, as shown and with no parse errors.

Edit:, using OP's code and adding an echo inside the function.

Just add the echo in the function:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
<?php 
function randomString() {

echo "Your code is: ";

    return rtrim(base64_encode(md5(microtime())),"=");

}

// echo isset($_GET['userid']) && !empty($_GET['userid']) && isset($_GET['transactionid']) && !empty($_GET['transactionid']) ? randomString() : "ERROR";


echo isset($_GET['userid']) && !empty($_GET['userid']) && isset($_GET['transactionid']) && !empty($_GET['transactionid']) ? randomString() : "ERROR";

?> 
 </body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Just check if the variable/value is available, while using a ternary operator.

<?php 
function randomString() {
   return rtrim(base64_encode(md5(microtime())),"=");
}

echo isset($_GET['userid']) ? randomString() : "ERROR";
?>

Is it that simple if then else what you're asking for??

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jeff
  • 6,895
  • 1
  • 15
  • 33
  • yes that is almost it. However the random value is still being generated even if the parameter `userid` has no value. How would test if `userid` has a value, if yes generate random string, if not "ERROR" – adrian.m123 May 16 '15 at 01:21
  • then change to `(isset($_GET['userid'] && $_GET['userid']>"") ? ....` this way you're also checking if there is not only an "empty" userid, but also a value greater than an empty string – Jeff May 16 '15 at 01:24
  • @adrian.m123 this answer works and using a ternary operator; it doesn't lie. Your GET array is obviously failing you somewhere and you need to show the good people, your actual code. – Funk Forty Niner May 16 '15 at 01:42
  • @Fred-ii- yes, I'm sure Jeff's answer above works. I'm sure I'm making a mistake. Please find current code above (after suggestions) – adrian.m123 May 16 '15 at 01:51
  • @adrian.m123 Try a double ternary then `echo $id = isset($_GET['userid']) ? $_GET['userid'] : isset($_GET['transactionid']) ? $_GET['transactionid'] : 'ERROR';` could work. Or, replacing `isset()` with `!empty()` – Funk Forty Niner May 16 '15 at 01:56
  • @Fred-ii- I think I'm entering Jeff's code incomplete or with errors. He suggested `echo (isset($_GET['userid'] && $_GET['userid']>"") ? randomString()) : "ERROR";` is that correct? – adrian.m123 May 16 '15 at 02:03
  • @Jeff can you edit your answer to include your last suggestion please? I' getting a parse error and i'm not sure why – adrian.m123 May 16 '15 at 02:06
  • @Jeff I found the error and works as desired. Thanks!!! – adrian.m123 May 16 '15 at 02:39
  • @adrian.m123 found "what" error? Here I wrote up an answer too. If you found the error, you need to tell us what that was. – Funk Forty Niner May 16 '15 at 02:40
  • @Fred-ii- I was putting an extra `(`before isset when it should have been simply `echo isset($_GET['userid'])...` I was entering `echo (isset($_GET['userid'])...` and never closed that `(`. I have updated the question with the code that worked as desired – adrian.m123 May 16 '15 at 02:59
  • @adrian.m123 The answer you accepted with their code and what you posted as an edit, doesn't match the solution. Two other answers were given, including one of mine. You can do whatever you want, but future visitors to the question/answer, will see that differently. You borrowed from different comments/answers to make it your own. – Funk Forty Niner May 16 '15 at 03:01
  • @Fred-ii- Your answer helped me too. How can I credit you and the other members who provided answers? The page allows me to add only 1 green check mark – adrian.m123 May 16 '15 at 03:04
  • @adrian.m123 It's ok, the points are ok, but I'm not in this for points. The ultimate goal is always "provide a solution". If other visitors to the question happen to upvote mine, great. If not, it's no biggie. – Funk Forty Niner May 16 '15 at 03:10
  • @Fred-ii- Please add a reply to the question so that I can set the check mark. I'm actually wondering how could I add a text tag before that random value, so that the output shows something like `Your Code: t5jfdkdd54` instead of the random value directly – adrian.m123 May 16 '15 at 03:18
  • @adrian.m123 I don't understand what you mean. – Funk Forty Niner May 16 '15 at 03:31
  • @Fred-ii- When I call the URL with all required parameters it shows a random value, something like `gDTsd38d9` alone. I would like to add a name before that value so that appears `Your Code: gDTsd38d9` but this name should appear only if the random value appears otherwise only `ERROR` – adrian.m123 May 16 '15 at 03:37
  • @adrian.m123 so, just add the echo in the function, *done like dinner* ;-) – Funk Forty Niner May 16 '15 at 03:38
  • @Fred-ii- Thanks Fred! Please add a reply so that I can credit you for your help :-) – adrian.m123 May 16 '15 at 03:43