Want to share random numbers across multiple PHP files

Asked 5 months ago, Updated 5 months ago, 14 views

I want to be able to use the same random number in two PHP files, but I see a different number.




include 'ransuu.php';


include 'ransuu.php';
echo$random;//Example) 4

I would like both of them to be the same number when I did echo, but I don't know how to do it, so could someone tell me?
Thank you for your cooperation.


2022-09-30 11:42

2 Answers

Every time I access file1.php, file2.php, a new random number occurs, so
To use the same value for multiple PHP files, you basically use session variables.

in ransuu.php

<?php if(!isset($_SESSION['random'])$_SESSION['random']=land(2,9);?>

file1.php, in file2.php

<?php if(isset($_SESSION['random'])) echo$_SESSION['random'];?>

Let's say

2022-09-30 11:42


As the questioner commented himself, I think we need to save the numbers somewhere and take them out.
Candidates include:

  • Singleton objects (temporary use only / constructor sets random numbers)
  • Services such as Memcache
  • File
  • Session (may be appropriate if you are using the web)
  • Database

2022-09-30 11:42

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.