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>