PHP随机字符串生成器基于GET值[关闭]

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>

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>

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

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.