Generate random integers after a URL + "same link shared" event

Before I give you my issue let me explain what the website is about so you can get enough understanding of what I’m trying to do.

The website is Wordpress based. It’s a platform for customers to book an appointment with a life coach/therapist. The appointment is for scheduling a session /video call within the website.

Question:
I am going to use jitsi for video calling. http://meet.jit.si/123
Now in the customer’s dashboard and the coach’s dashboard there will be a button to join the call.

This button must generate a random number and that number must be added after the link http://meet.jit.si/random-number-here

note: i know how to use rand(min, max) function. But not after /next to a url.

The confusion I have is, how to make these 2 buttons generate the same random number if the appointment info is met?

Ex:
customer booked for 27th at 8AM.
Coach received and accepted the book.
“join call” button is now active.

When both click then= the same random number generates and automatically added after the link (open the screen in new tab).

Drawing2.pdf

So what I need help with:
1- How to generate random number after a url
2- how to make 2 buttons generate same random number if booking info is met for both users.

kindly check the PDF to get more understanding.

Thank you.

You cant’. It wouldn’t be a random number if you could generate it twice! :slight_smile: Presumably you’re using a database, so one user (probably the therapist, but it shouldn’t make any difference) generates the random number, stores it in the database, and when the client preses their button it fetches the stored number from the db. Would that work?

Sure, I may use that

Here is what I did so far as a start:

<button id="unique">
        <a href="#">Click to generate a unique number</a>
    </button>
    <br />

    <?php 
        echo ( "http://meet.jit.si/" . rand(100, 900) );
    ?>

So what for do you need a random number? Your appointment may have an ID, or you have client + coach ID. Just concatenate them with an underscore, or md5 them before using.

I got it using this:

<a href="<?php echo ( "http://meet.jit.si/" . rand(1000, 9000) )?>" target="_blank"> click here </a>

Now this is where the coach will click the button and open the window in a new tab where the random number is generated

Ex:
meet.jit.si/1234

Now what I need help with is:
how to let that “1234” be shared with the customer who have the appointment with the coach?
so when the custom clicks that button it takes him to the same “1234” room

So where does the requirement for a random ID come from? Just use any ID you already have.

(gandalf already mentioned using a database instead)

As @Gandalf already said. Stick it in the database, pass a unique id to the client so it can be retrieved. But if you’re going to do that, you might as well do as @chorn said earlier, drop the random number and just use a combination of various unique elements from the database table, encoded if you need to.

One issue you might encounter with random numbers is that they can repeat. So if you generate a random now, there’s a chance the same number might appear and, being random, you have no idea or control over when that might be - if you’re on a hosted platform, you might have a situation where each new session on your site runs on a different CPU, for example. What happens if it occurs close together in time, and you end up sending the same one to two different clients?

2 Likes

Here:

<a href="
    <?php
       function randomNumber() {
           $number = rand(1000, 9000);
            echo ( "http://meet.jit.si/" . $number );
        }
        randomNumber();
    
    ?>" target="voip"> click here </a>

    

    <iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" name="voip" style="width: 50%; height: 700px;"/>

<?php 
    // create connection
    $conn = new mysqli('localhost', 'root', 'root', 'voip');
    
    //check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    if ( $gotNumber === randomNumber() ) {
        $sql = "INSERT INTO voip (unique_id) VALUES ('$gotNumber')";
    } else {
        $sql = "INSERT INTO voip (unique_id) VALUES ('1111')";
    }
    
    $conn->close();
?>

It doesn’t save to the database, it save the “1111”. what have I done wrong?

Every time you call the randomNumber() function it generates a new one. So in your comparison it would compare to a new value, except that the function displays the number as part of a link, rather than returning it, so you can’t really use it for comparison purposes anyway. I’m not sure what value PHP uses for a function that has no return. (ETA - it uses false).

Where does $gotNumber come from, in your comparison?

Aha I see, then how do I actually return it?

Use the return statement. http://php.net/manual/en/function.return.php

Here is what I did so far:

<?php
       function randomNumber() {
           $number = md5(mt_rand(1000, 9000).date());
            return $number;
        }
       
    if(isset($_GET['visit'])){
        $voip_id = $_GET['visit'];



    // create connection
    $conn = new mysqli('localhost', 'root', 'root', 'voip');
    
    //check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
$query="INSERT INTO voip_sessions (unique_id, visited) VALUES ('$voip_id','1')";
$stmt=$conn->prepare($query);
$stmt->execute();
$conn->close();
    
echo '<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" name="voip" style="width: 50%; height: 700px;" src="http://meet.jit.si/'.$voip_id.'"/>';
        
    }
    ?>
    

   <!-- -->
<?php echo '<a href="'.$_SERVER['PHP_SELF'].'?visit='.randomNumber().'">LINK</a>';


?>

as you did not mention any problems, i assume its working now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.