/    Sign up×
Community /Pin to ProfileBookmark

Random open url one then two then three witp php

Hi, Wath i want is a simple php file with some different urls in it.
When accesing the php file first open url one.

When accessing the php file again opening link two.
And so on, when all are done start over again.

I have the below code. I have tried it but it does random opens the same urls.

Can anyone help me with the code? I have little to no experience. But if i have the right code i can make it work myself.

<?php

$link[1] = “#”;

$link[2] = “#”;

$link[3] = “#”;

$link[4] = “#”;

$link[5] = “#”;

if(!isset($HTTP_cookie_VARS[‘link’])){ $n=count($link);

$rand=rand(1,$n); setcookie(“link”,$rand,time()+3600);

header(‘location:’.$link[$rand]); }else{ $go=$link[$_COOKIE[‘link’]]; header(‘location:’.$go); }

?>

Kind regards
Melissa

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@rootMay 18.2015 — 
  • 1. when posting code, please use forum tags, in this case PHP ones.


  • [code=php]<?php
    $link[1] = "#";
    $link[2] = "#";
    $link[3] = "#";
    $link[4] = "#";
    $link[5] = "#";
    if(!isset($HTTP_cookie_VARS['link'])){ $n=count($link);
    $rand=rand(1,$n); setcookie("link",$rand,time()+3600);
    header('location:'.$link[$rand]); }else{ $go=$link[$_COOKIE['link']]; header('location:'.$go); }
    ?>[/code]


    If all you need is a link and a cookie to keep a reference of that link, then a simple session cookie would suffice and you could alter the code to something like...
    [code=php]<?php
    session_start();
    $link = array(
    1=>"#",
    2=>"#",
    3=>"#",
    4=>"#",
    5=>"#"
    );
    $rand = $_SESSION['link'] = !isset($_SESSION['link'] ) ? rand(1, count( $link) ) : $_SESSION['link'];
    header("location: {$link[$rand]}");
    ?>[/code]
    Copy linkTweet thisAlerts:
    @adkwauthorMay 18.2015 — 1. when posting code, please use forum tags, in this case PHP ones.

    [code=php]<?php
    $link[1] = "#";
    $link[2] = "#";
    $link[3] = "#";
    $link[4] = "#";
    $link[5] = "#";
    if(!isset($HTTP_cookie_VARS['link'])){ $n=count($link);
    $rand=rand(1,$n); setcookie("link",$rand,time()+3600);
    header('location:'.$link[$rand]); }else{ $go=$link[$_COOKIE['link']]; header('location:'.$go); }
    ?>[/code]


    If all you need is a link and a cookie to keep a reference of that link, then a simple session cookie would suffice and you could alter the code to something like...
    [code=php]<?php
    session_start();
    $link = array(
    1=>"#",
    2=>"#",
    3=>"#",
    4=>"#",
    5=>"#"
    );
    $rand = $_SESSION['link'] = !isset($_SESSION['link'] ) ? rand(1, count( $link) ) : $_SESSION['link'];
    header("location: {$link[$rand]}");
    ?>[/code]
    [/QUOTE]


    Thank you for your tips.

    It is showing these errors.

    [code=php]
    Warning: session_start(): Cannot send session cookie - headers already sent by (output started on line 2
    Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at line 2
    Warning: Cannot modify header information - headers already sent by (output started at line 29[/code]
    Copy linkTweet thisAlerts:
    @rootMay 18.2015 — [b]"Cannot send session cookie - headers already sent"[/b] by (output started on line 2 is a clue, it says that your server has already sent the HTML, this would need to be a session started on line 1 of your document, for example

    [code=php]<?php
    session_start();

    ...
    ...
    ...


    ?>
    <html>
    <head>
    ...
    [/code]



    What is your code block from lines 1 through to 30 as you currently have it, it will help to understand the top of your page, what is sent, how it is sent, set up, etc.
    Copy linkTweet thisAlerts:
    @rootMay 18.2015 — You could try an alternative approach, using HTML's ability to redirect.

    Untested...
    [code=php]<?php
    session_start();
    $link = array(
    1=>"#",
    2=>"#",
    3=>"#",
    4=>"#",
    5=>"#"
    );
    $rand = $_SESSION['link'] = !isset($_SESSION['link'] ) ? rand(1, count( $link) ) : $_SESSION['link'];

    ?>
    <html>
    <meta http-equiv="refresh" content="3; url=<?php echo "{$link[$rand]}";?>" />
    ...
    ...
    </head>
    <body>
    Please wait, taking you to <?php echo "{$link[$rand]}";?>
    ...
    ...?>
    [/code]
    Copy linkTweet thisAlerts:
    @adkwauthorMay 18.2015 — Hi,

    I exactly use your code.

    Only put some wp all import trigger links in it.

    This code is very nice, only it uses only the last url each time.

    For an example i want 2 files.

    File one with Trigger urls, and acces them all once per 24 hours. I will acces the file with cronjobs.

    File two with Processing urls , i will trigger that file every 5 minutes with cronjob.

    Here are example urls from wp all import.

    I really like you are helping me so far ?
    You could try an alternative approach, using HTML's ability to redirect.

    Untested...
    [code=php]<?php
    session_start();
    $link = array(
    1=>"#",
    2=>"#",
    3=>"#",
    4=>"#",
    5=>"#"
    );
    $rand = $_SESSION['link'] = !isset($_SESSION['link'] ) ? rand(1, count( $link) ) : $_SESSION['link'];

    ?>
    <html>
    <meta http-equiv="refresh" content="3; url=<?php echo "{$link[$rand]}";?>" />
    ...
    ...
    </head>
    <body>
    Please wait, taking you to <?php echo "{$link[$rand]}";?>
    ...
    ...?>
    [/code]
    [/QUOTE]
    Copy linkTweet thisAlerts:
    @adkwauthorMay 18.2015 — Here are the example urls

    The trigger URL will look something like this:

    http://YOUR-WEBSITE.com/wp-cron.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=trigger

    The processing URL will look something like this:

    http://YOUR-WEBSITE.com/wp-cron.php?import_key=[YOUR_SECRET_KEY]&import_id=[YOUR_IMPORT_ID]&action=processing

    Hi,

    I exactly use your code.

    Only put some wp all import trigger links in it.

    This code is very nice, only it uses only the last url each time.

    For an example i want 2 files.

    File one with Trigger urls, and acces them all once per 24 hours. I will acces the file with cronjobs.

    File two with Processing urls , i will trigger that file every 5 minutes with cronjob.

    Here are example urls from wp all import.

    I really like you are helping me so far ?[/QUOTE]
    Copy linkTweet thisAlerts:
    @rootMay 19.2015 — So you want a server to initiate these calls to the urls when a user visits your server, am I correct?

    ... and these urls are randomly chosen.

    What I don't understand is the reference to triggers and doing something with 2 files... sounds like your venture is overly complicated.

    Why not explain what will happen when I visit http://yourwebdomain.com/ what will I see, how did that selection happen, was it via some marketing system, cookies, what?

    or is this system a web server on your PC and you want to visit a web page on your server to do whatever it is your trying to do..?

    you could explain what a trigger does and where it does it and what you expect to happen when you have triggered the trigger?
    Copy linkTweet thisAlerts:
    @adkwauthorMay 19.2015 — Hi,

    I use Wordpress as Affiliate websites.

    So they are filled with Affiliate products.

    I do this with WP All Import from www.allimport.com

    To update the feeds you need to trigger the url once per 24 hours http://nuwitgoed.nl/wp-cron.php?import_key=YSBK4xjS1&import_id=2&action=trigger

    After that url is triggered you need to Process these url http://nuwitgoed.nl/wp-cron.php?import_key=YSBK4xjS1&import_id=2&action=processing

    The processing url can be activated until the whole feed is updated.

    That is all i want to do.

    Does this info explain enough?

    So you want a server to initiate these calls to the urls when a user visits your server, am I correct?

    ... and these urls are randomly chosen.

    What I don't understand is the reference to triggers and doing something with 2 files... sounds like your venture is overly complicated.

    Why not explain what will happen when I visit http://yourwebdomain.com/ what will I see, how did that selection happen, was it via some marketing system, cookies, what?

    or is this system a web server on your PC and you want to visit a web page on your server to do whatever it is your trying to do..?

    you could explain what a trigger does and where it does it and what you expect to happen when you have triggered the trigger?[/QUOTE]
    Copy linkTweet thisAlerts:
    @rootMay 19.2015 — Do the trigger URL's return kind of anyvalue to your server?

    I would look at the cURL manual on the PHP website, that might be more in the area you need, they can cope with various needs.
    Copy linkTweet thisAlerts:
    @adkwauthorMay 19.2015 — Thank you for all your information.

    Unfortunatly i don`t understand PHP codes that well.

    I will think of a better plan and try find someone who can write me a tool for it.

    Kind regards

    Melissa
    ×

    Success!

    Help @adkw spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.13,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...