Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Visitors on server (maneshr)

Posted on 2000-04-19
Medium Priority
Last Modified: 2013-12-25
Hope you did not forget me! i am the one from question :

I was talking about a strange script that i saw.... which prints a number dinamically and randomly into the screen, as soon as executed!

This is interesting because it will make some visitors, feel that my site has alot of visitors.
As soon as the visitors come in, he/she finds a statement in the page that says something like: there are 34 visitors on this site right now.

and if he reload he will find another number (as if there are people coming in or out) and so on.   I belive that this should be done by a cgi script NOT java, for many reasons: java code will be seen by the visitors if they view the source, but not the .cgi file! also Java is embedded into the page which will make it heavier, where the cgi is NOT actually inside the page, it is just executed somewhere.

But, they did that script in such a "dump" way... the script says 5 visitors, and if i reload within 2 seconds it says 512 visitors , then reload, then 37 visitors, then 419 then 600 then 701 then 41

Which does not make sense... I am SURE that they are not getting these kind of dynamic traffic in their whole life :)   i would die to remember that site again, thought i bookmarked it ... but i didn't :(

Anyway, your code:

$random = int( rand( $upper-$lower+1 ) ) + $lower;
print $random,"\n";

Is a VERY good start, but as we agreed it needs some work to "neutralize" the operation .... i.e. make it look normal.

I need your opinion about how can we do this? your thoughts are VERY important for me.

Here are some ideas that surly needs to be refined:
1) We can set several groups of numbers..... each group contains a few numbers ....the script will randomly select a group.... then randomly select a number from inside that group.... then STICKS to the numbers in the selected group ONLY  as much reloads it will only display random numbers form the originally selected group!
so group A "12,13,14,15,16,17,18"
group B   "215,216,217,218,219,220" and so on..... each group represents an assumed amounts of trafic and load.

Also, instead of selecting the "group" randomly, it can depend on the users clock ... if the time is between 00:00 and 04:00 group number XX will be chosen then , numbers inside it will be randomly displayed...

 2)  The random number can be "calculated" based on the user's clock! if it says  11:38  , we can take the  38 (the mutinies) and add/deduct (randomly) some other numbers .... or multiply by 1.1 so the new number become 42 then when reload (within a minute) no change will be viewed, but when reloading after a minute , the new number will be another number (cuz, of the change of the user's minutes!)

3)I need some innovated ideas from you!   :-)
Question by:windowsssss
  • 10
  • 8
  • 2
  • +1
LVL 16

Expert Comment

ID: 2730898

2) i think the system time on the users machine is not reliable since it can be changed by the user. Instead we could use the server time, which is much more reliable. Also to get the users system time, we might have to use javacript and that would defeat the whole purpose (as you have mentioned with using Java).

3) here is what i have thought of.

* create a list of ranges.
* next generate a random number between the list of values.
* select a random item from the list. for that item get the range.
* generate a random number between that range.
* show it to the user.

Let me know how that sounds and i will get the code ready.

Author Comment

ID: 2731735
Ok... it really sounds good.... there are two points:

1) I want to make sure that the ranges will be randomly shosen, for ONE time per session.    
IN other words, if in the first ime to log into the page, range number 7 is shosen, which contains numbers from  130-137 ..... so each time the browser reload the number will be in that range 130-137...... see, no change in the range itself for the same session.

When the user close the window, the session ends up... so if he come back.... ok, a new range will be selected then stick to numbers inside that new range... etc...

2) I Want it to be easy for me, to play with:
A)The list of ranges,
B)The range inside each list.

I suggest: creating two files.... one is the script itself, what the program will do... and the second is the one with the lists and ranges (in a format that can be easyly altered).

3) For easy positioning i suggest that i would put something like: <!--#exec cgi="/cgi-bin/FILE_NAME.cgi"
in somewhere in the code.... this will simplify the instalation for me.

N.B. well, thats all i think, BUT maneshr , i want to ask you about something and need you to contact me if you can (there are things that i don't think will be good to be posted her). please contact me on sarah_jade_1999@yahoo.com
Your email will not be upused (i promise)  :)

Thank you very much.
LVL 16

Expert Comment

ID: 2731843
1) its difficult to implement that in a fairly simple way.
i would suggest another easier solution.

create a HTML page with 2 frames. the bottom frame will have the hit counter and the top page will have the content. now when the user clicks on any link in the upper frame/content page the lower frame will remain as it is!! this will give the feeling that the counter is static.

if you instead change the number to something else, the user might immediately suspect something fishy.

2 & 3) i am sure you will find it very easy to maintain that code.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 2732840
maneshr , Frames are BAD! first , many users find it confusing... also , a lower frame, will waste a valuable space of the screen.
Frames are a bit confusing specially when somone wants to print something.... many users does not know that they must  click somewhere in the window to print a frame.

ok... lets go back to yur main idea... using the server-side clock.
If the minutes in the clock are from 00 to 15 we use list number 1 that contains numbers from 20 to 25 (for example).
If minutes are 16 to 30, use a random number from numbers in list 2 (e.g.  26 to 30)  and so on......

IMPORTANT: again, i must have an easy way to modify the lists (according to munites in this case), AND the range inside each list.

Please tell me what do you think about that.

I belive that i have a script that prints the server's clock (you may use it as a start template). I can email it to you,ifyou prefer.
LVL 17

Expert Comment

ID: 2734409
Hi, i'm sorry to "barge in", but I was hoping someone could help me with a perl script.
I run Apache on RedHat 6.2, and I do my webs on Dreamweaver2 on a Windows box. I am trying to load a cgi script written in perl (access counter), but nothing happens!!!
in httpd.conf, I have Options ExecCGI Includes for the virtual web.
The cgi-bin dir is in /home/httpd/cgi-bin
and my webs lie in /home/httpd/html/webs
According to the instructions, I should add <!--#exec cgi="/cgi-bin/count.cgi" --> to a page's html code, and then edit the script to make it visible etc.
I do everything the instructions say, but it is as if the cgi script just doesn't get executed. The script writes to a count.html file. When however I execute the script manually (perl /home/httpd/cgi-bin/count.cgi)
everything works and the count.html is updated, just not when it should execute from the web???!!!
Could you pleassssseeee help me?


Author Comment

ID: 2734743
Hi psimation ,
Actually, i don't know the answer for your question.... but i suggest, that you post a fresh question in the main cgi page.... this will enbale more experts to see your question... and moreover, will encourge them to answer , as they will get the points from you.

But posting your question inside this question, is going nowhere!  simply because you are not deposting any points, and the points in this question are for answering my question. :)
Good luck.

Expert Comment

ID: 2737361
Ummm, hope windowsssss won't mind me answering psimation on his/her account.

Psimation, please make sure your count CGI script can be executed by everybody. (chmod a+x /home/httpd/cgi-bin/count.cgi)
Can you run other CGI scripts from the web? If yes, then do as above, else, you gotta play with httpd.conf SERIOUSLY!

Have fun!
LVL 16

Expert Comment

ID: 2740406

windowsssss is right. Pl post a seperate Q in the PERL forum at...

That way if chaduka has answered your Q, you can give him/her the points!!

Thanks :-)
LVL 16

Expert Comment

ID: 2740646
i agree that as far as printing is concerned, frames are bad, but in this case the frame that shows the random number will be small and take up only a small part of the browser window.

Anyway lets leave this issue for the end. lets focus on getting the random numbers in the first place.

i have a Q about the logic you have mentioned (ok... lets go back to yur main idea... using the server-side clock.
                    If the minutes in the clock are from 00 to 15 we use list number 1 that contains numbers from
                    20 to 25 (for example).
                    If minutes are 16 to 30, use a random number from numbers in list 2 (e.g.  26 to 30)  and so
                    on...... )

This logic will make the whole effect of random predictable, since we will definately know the range from which a random number will be picked depending on the minutes on the server clock!!

i have a code ready that will look at a list. this list has many ranges of numbers in it.

it will first pick up a random item number in this list. then it will find out the range of numbers for that item number. next it will generate a random number in that range.

The advantages of this approach are...

1) you dont depend on system date-time in any way. this makes your code very portable. so tomorrow if you have a Windows NT server the same code will work.

2) since the whole process uses random nos, there is no way even we could predict what number will be shown to the user, thus making the code really, really random.

i have written the code in such a way that you will find it VERY EASY to maintain and add your own ranges. Also i have added a lot of comments, so that you can give this code to any other person who knows PERL and it will be easy for that person to undertand what the code does.

So if the above is fine with you, i will post the code here in this forum.

let me know


Author Comment

ID: 2751594
Maneshr, sorry for the dely!  well, your suggestion sound good.... but, the reason i wanted the system to depend on the server's clock, is that to prevent chosing a list that has a very diffrent range from the precous one!

let me explian: if the browsere select list #5 that contains numbers from 50 to 60..... and the selcetd number was 63.

Now, when the user reload , the browser will select another list... which may be list #1 that contains numbers from 1 to 10..... so if the selcted number is 2 (for example) this would be a problem!!!

the visitors were 63 and whithin less than a minute, they become 2 !  :)

so, i want to know how can we solve such a situation?

as suggested before: i hope that , when the browser select a list.... it will always chose from this list in the same session (i.e. till closing the browser)..... so this will be realistic..... if the browser sellect the list #3  that contains 20-30, so the fist selcted number will be 24 the second may be 20 the third may be  27 the fourth will be 22... see, this is more realistic, and easier to belive.

Please tell me what do you think about that?
LVL 16

Expert Comment

ID: 2751875
i would then suggest using either the frame approach that i mentioned earlier or some cookie based system.

The problem with doing a random no generation for the SAME user for a 2nd time will only make the application script complicated.
Rather than that i would suggest that the script only focus on generating a random number and leave the subsequent display of the number to a seperate logic (frame/cookie or anything else).

Also introducing the system clock in the picture would only make the result that much more predictable. That kind of defeats the whole purpose of having to generate random numbers in the first place.

let me know your thoughts.

Author Comment

ID: 2771467

Ok Maneshr .... do the system as you siad in the above comment... please include simple installation procedures. thanks. :)
LVL 16

Expert Comment

ID: 2773574
give me some time. since you did not reply, i deleted the scripts i had created. i will re write them today.

LVL 16

Expert Comment

ID: 2773741
OK! i finally managed to trace the file.

here it is.

* copy the file in your cgi-bin directory.
* change the file permission to 755.

Thats it!! you are ready to go!!


* No external PERL modules used. Therefore this has to work on your system, provided basic version of PERL is installed.
* Protable across systems. So you dont have to worry about which OS the script is running on.



##  Shuffle the array
shuffle( \@array );

##  Generate a random number between 1 and no of elements in the array.
$random= int( rand( $[- $#array+1 ) ) + $#array;

$random = int(gaussian_rand() * $lower+ $upper);
#$random = int(rand( $upper-$lower+1 ) ) + $lower;
print $random,"\n";

# shuffle( \@array ) : generate a random permutation
# of @array in place
sub shuffle {
  my $array = shift;
  my $i;
  for ($i = @$array; --$i; ) {
    my $j = int rand ($i+1);
    next if $i == $j;
    @$array[$i,$j] = @$array[$j,$i];

sub gaussian_rand {
  my ($u1, $u2);  # uniformly distributed random numbers
  my $w;          # variance, then a weight
  my ($g1, $g2);  # gaussian-distributed numbers

  do {
    $u1 = 2 * rand() - 1;
    $u2 = 2 * rand() - 1;
    $w = $u1*$u1 + $u2*$u2;
  } while ( $w >= 1 );

  $w = sqrt( (-2 * log($w))  / $w );
  $g2 = $u1 * $w;
  $g1 = $u2 * $w;
  # return both if wanted, else just one
  return wantarray ? ($g1, $g2) : $g1;
LVL 17

Expert Comment

ID: 2774223
Sorry, me barging in again. I placed a new question in the CGI section (same as this question), could you guys please take a look at it, I just know that between you all I'll get the answer.
LVL 16

Expert Comment

ID: 2774473

pl. check my reply. :-)


Author Comment

ID: 2812593
Hi maneshr ,
well, i don't know if i did something wrong... but the file gave me an error message!
i enserted the following line into the html file:
<!--#exec cgi="/cgi-bin/random.pl"
where my sgi directory is cgi-bin.

and i put the random.pl inside that directory!  am i doing something wrong?
LVL 16

Accepted Solution

maneshr earned 480 total points
ID: 2813907
what error message did you get??
are the permssions on the file proper??

can you execute the script from the command prompt??

what ouput does it give you?

the SSI directive should be..
<!--#exec cgi="/cgi-bin/random.pl"-->

Author Comment

ID: 2894798
Thanks manesh,

hope to hear from you again... and soon! :)

Author Comment

ID: 2894830
Comment accepted as answer
LVL 16

Expert Comment

ID: 2897819
"...hope to hear from you again... and soon! :).."

Sure. do keep in touch

Warm regards,

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question