Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Javascript redirect problem ; (

    Hey guys, so my situation is as follows:

    I wrote a submission system in php that writes to a textfile rather than a database, the idea of the system is people submit their url to the textfile and then when that script is called on a page, it redirects to a random address out of the textfile; the problem is, I don't know how to make javascript read from the text file and then pick a line to redirect to.

    Actually, just to clarify, I know how to make javascript read from the text file; but I have NO idea how id write a function to pick a url from the file and forward to it.

    Seeing as I hit this road block a couple of days ago, the only way I have been handling submissions is checking the text file every 12 hours for new submissions and then manually adding them to this code:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    setTimeout(function() {
    var howMany = 38; 
    var page = new Array(howMany+1);
    
    page[0]="http://gproxy.nl/";
    page[1]="http://homeproxy.me/";
    page[2]="http://proxyturbo.com/";
    page[3]="http://www.lblocker.info/";
    page[4]="http://goprivate.eu/";
    page[5]="http://jsproxy.com/";
    page[6]="http://openthis.eu/";
    page[7]="http://proxy4home.info/";
    page[8]="http://dedicatedipaddress.net/";
    page[9]="https://www.4everproxy.com/";
    page[10]="http://www.surfsearch.info/";
    page[11]="http://www.leaveproxy.com/";
    page[12]="http://proxyecole.fr/";
    page[13]="http://newipnow.com/";
    page[14]="http://www.hiddenmode.info/";
    page[15]="https://europrox.org/";
    page[16]="https://www.4everproxy.com/";
    page[17]="https://goingthere.org/";
    page[18]="http://xuxor.com/";
    page[19]="http://033b.com/";
    page[20]="http://thewebtunnel.com/";
    page[21]="http://prox.phanteye.com/";
    page[22]="http://www.hiddenall.info/";
    page[23]="http://www.5966.info/";
    page[24]="http://hideyoself.com/";
    page[25]="http://prox.phanteye.com/";
    page[26]="http://freevideoproxy.com/";
    page[27]="http://thewebtunnel.com/";
    page[28]="http://openthis.eu/";
    page[29]="https://europrox.org/";
    page[30]="http://xuxor.com/";
    page[31]="https://incloak.com/";
    page[32]="http://www.leaveproxy.com/";
    page[33]="http://www.openunblocker.com/";
    page[34]="http://post48.com";
    page[35]="http://post48.com";
    page[36]="http://inteproxy.com";
    page[37]="http://208.73.23.59";
    page[38]="http://hidemetoday.com/";
    
    
    function rndnumber(){
    var randscript = -1;
    while (randscript < 0 || randscript > howMany || isNaN(randscript)){
    randscript = parseInt(Math.random()*(howMany+1));
    }
    return randscript;
    }
    quo = rndnumber();
    quox = page[quo];
    window.location=(quox);
    }, 1500);
    </SCRIPT>
    I would be very grateful if someone would help me write the script or tell me what kind of function I should be googling to look up, googling "How to make javascript read from a textfile and redirect" doesn't really turn up much ; (

    Many thanks!

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Is there any specific reason you want to use Javascript for this? Seeing how you are already using PHP, it'd be much easier to just use PHP to read the file and do the redirect (by setting a header).

    Also, please keep in mind that this is a great security issue as people can easily make other people go to malicious websites without them being able to do anything about it. It might even be a legal issue on your end.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Agree with Airblader: Makes no sense at all to do this in JavaScript instead of in PHP.

    Even if you don't want the redirect to happen until the user has been on the page for some time (or when some page event happens), you could/should still use PHP to pick *ONE* of the URLs (at random) and pass that into your HTML as the one and only URL this particular user will see.

    Having said all the above: If you REALLY must do this, and you say you know how to read the file with JavaScript: Just read the entire file into a single string, split the string on the newline character ("\n") to produce an array of strings, and pick one element of that array at random.
    Code:
    var reader = new XMLHttpRequest();
    reader.open( "GET", "filename.txt", false );
    reader.send();
    var lines = reader.responseText.split(/\r?\n/);
    var url = lines[ Math.floor( Math.random() * lines.length ) ) ];
    Why work harder?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •