douwulu2576 2015-05-16 00:48
浏览 51
已采纳

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>
  • 写回答

3条回答 默认 最新

  • duanji5569 2015-05-16 02:34
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料