how to generate random non repeated numbers in php

Joined
Jun 30, 2005
Messages
59
Reaction score
0
Hi all. could an expert show me how i can generate random non repeated numbers. I want to use those number as file name that i write to. currently the file name is ram.txt but i want it the file name to be like 948450913.txt each time the scripts runs diffrent number .I be happy if an expert show me how to do that here in this code?thanks

Code:
<?php


[B]$url[1] = "[url="http://localhost/flash_mp3_player/mp3/08"]http://localhost/flash_mp3_player/mp3/08[/url] - Track 8.mp3";
$url[2] = "[url="http://localhost/flash_mp3_player/mp3/13"]http://localhost/flash_mp3_player/mp3/13[/url] - Gar Aya.mp3";
$url[3] = "[url="http://localhost/flash_mp3_player/mp3/Soroush"]http://localhost/flash_mp3_player/mp3/Soroush[/url] - Yeh Donya - 02 Shoghe Nafas.mp3";[/B]

$mycontent = "";
if (isset($_GET["sid"]))
{
   $allsid = explode (",",$_GET["sid"]);
   $mycontent = array();
   foreach ($allsid AS $value)
	  $mycontent[] = $url[$value];
}

echo $mycontent;

[B]$handle = fopen ("ram.txt","w+");[/B]
if ($handle)
{
  if (fwrite ( $handle,implode("\r\n",$mycontent)."\r\n") )
  {
	  echo "FILE IS WRITTEN SUCCESSFULLY";
  } else
  {
	   echo "ERROR IN WRITING TO FILE";
  }
  fclose ($handle);
} else
{
	  echo "ERROR IN OPENING FILE";
}
require 'config.txt';
?>
 
Joined
Mar 17, 2005
Messages
159
Reaction score
0
yes - random numbers do repeat,.

here's some code but seriously I don't think you want to do this as it WILL break badly

$timestampfilename = time() . '.txt';
$handle = fopen ($timestampfilename,"w+");

perhaps if you can tell us what you want to do then more useful / less broken suggestions can be advised :)

Sil
 
Last edited:
Joined
Jun 30, 2005
Messages
59
Reaction score
0
silver said:
yes - random numbers do repeat,.

here's some code but seriously I don't think you want to do this as it WILL break badly

$timestampfilename = time() . '.txt';
$handle = fopen ($timestampfilename,"w+");

perhaps if you can tell us what you want to do then more useful / less broken suggestions can be advised :)

Sil

Thank u for u reply. I just try to create dyanmic play list. the text file holds the play list so i want to decrese chance that 2 user get same playlist!
 
Joined
Mar 17, 2005
Messages
159
Reaction score
0
do you actually need to store the file on the server?

with cgi / php you can click a button / press link and have the script output the data as if they were downloading from an actual file, does the playlist need to be a file?

Sil
 
Joined
Jun 30, 2005
Messages
59
Reaction score
0
silver said:
do you actually need to store the file on the server?

with cgi / php you can click a button / press link and have the script output the data as if they were downloading from an actual file, does the playlist need to be a file?

Sil
Thank u for u reply. Well the player is flash mp3 player and one way to let it play playlist is to feed it with .txt file so that i goes and playes all song inside it. I want each user get his own playlist and also avoid the browser catching which makes the same playlist play even if new playlist is created!!.I know there other ways but i do not know how to do it!! If u guys know of any good method i be happy if u guys tell me .Thank.

code for flash player :
Code:
<object type="application/x-shockwave-flash" data[b]="mp3player.swf?playlist=playlist.txt"[/b] width="280" height="280" wmode="transparent">
<param name="movie" value="mp3player.swf?playlist=playlist.txt" />
<param name="wmode" value="transparent" />
</object>
 
Last edited:
Joined
Mar 17, 2005
Messages
159
Reaction score
0
the reason not to use the timestamp above would be that diff users could in theory try making a playlist at the same time,. you can include the IP address as part of the filename - or use php's session Id - which I think is guarenteed to be unique (well pretty well unique most of the time)?

Sil
 
Joined
Jun 30, 2005
Messages
59
Reaction score
0
silver said:
the reason not to use the timestamp above would be that diff users could in theory try making a playlist at the same time,. you can include the IP address as part of the filename - or use php's session Id - which I think is guarenteed to be unique (well pretty well unique most of the time)?

Sil

Many thanks to u reply . could u show me how i can add ip address as part of the file name in php ? I do not know how to combine ip with file name . I assume beside ip some thing else should be combined for the file name since a user may decide to create new playlist and the old playlist might stay in browser catch and cause problem of playing old playlist !!
Code:
 [b]$filename[/b] = [b]time()[/b] . ".txt"; 
 
$handle = fopen ([b]$filename[/b],"w+");
 
Joined
Mar 17, 2005
Messages
159
Reaction score
0
use time + IP, it's still possible one user could make a playlist twice within a second - or less likely perhaps users sharing the same IP make a playlist at the same time,. to be honest though it's not such a big deal and the likelyhood is quite small?

Code:
 $filename = time() . $_SERVER['REMOTE_ADDR'] .  ".txt"; 
 
$handle = fopen ($filename,"w+");

something like that I think?

Sil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top