-3

I need a php code which i upload on my domain. what it will do is redirect visitors to different different urls based on certain time period like say for the first 10 mins it should redirect to google.com later on for the next 10 mins to gmail or something else whatever which i specify and so on. Is there is any way to do that coz right now i have to do it manually which is really hectic. All i got so far is this but its not working! `

$tym = date("h:i");
echo $tym;

if ($tym >= "06:10" && $tym < "06:20"){                                                 
$urls = array(
    array('url' => 'http://website1.com'),
);}

if ($tym >= "06:20" && $tym < "06:30"){                                                 
$urls = array(
    array('url' => 'http://website2.com'),
);}
//and so on

$probs = array();
foreach ($urls as $i => $url) {
    $probs = array_merge($probs, array_fill(1, $i));
}

$rand = array_rand($probs);
var_dump($rand);
var_dump($urls[$probs[$rand]]['url']);

header('Location: '.$urls[$probs[$rand]]['url']);`

Need some help please! Thanks in advance.

Dvsh
  • 19
  • 4
  • "not working" how, exactly? Explain what happens when you run the code...what time was it when you ran it? What result did you expect? What result did you actually get? An error, or some different outcome, or what? Give us a clue – ADyson Sep 24 '21 at 12:02
  • all i get is null null written just. i don't know whats wrong! – Dvsh Sep 24 '21 at 12:05
  • Well, what debugging have you done? Trace it back, step by step, to find where the variables start to not have the values you expected and/or the code doesn't take the path you expected. That's the first thing you should always do when you don't know what's wrong with the output. – ADyson Sep 24 '21 at 12:06
  • At the very least you should be seeing a warning because [array_fill](https://www.php.net/manual/en/function.array-fill.php) expects 3 parameters, not 2. – ADyson Sep 24 '21 at 12:09
  • can someone please correct the code and share it over here please! – Dvsh Sep 24 '21 at 12:13
  • 2
    We're not a free do-my-debugging service. See [ask]. **You** need to debug it and then if you have a specific error / problem for us to solve, if you can't fix one specific bit. You can ask for debugging help, but only if you're specific. You can't just shrug your shoulders, say "doesn't work", and then hand it over to us to debug the whole thing for free. If you don't know how to do basic debugging of your code, then that's a core programming skill which is missing from your skillset, and you need to correct that. It should be something you learn from the first minute you start to write code. – ADyson Sep 24 '21 at 12:15
  • 1
    What I will say though is: it's unclear what you're trying to achieve with this whole bit here anyway: `$probs = array(); foreach ($urls as $i => $url) { $probs = array_merge($probs, array_fill(1, $i)); } $rand = array_rand($probs); ` ...according to your examples, $urls can only ever have one entry in it at a time, so I don't know why a) you're expecting $urls to have information you can loop through, and b) why you're trying to put that into another array which will just end up with the exact same content, and c) why you're trying to select a random item from it. – ADyson Sep 24 '21 at 12:17
  • i am so sorry for the way but i have no idea how to debug it through as the code was working fine some time ago and now it stopped all of a sudden and all i am getting is null null written just which i don't know how it got there. I don't understand this rand code much as i am familier with just the basic php codes. That's why i was looking out for some help please. – Dvsh Sep 24 '21 at 12:22
  • 1
    `i have no idea how to debug it `...then please read http://www.phpknowhow.com/basics/basic-debugging/ as a starting point. If you can't debug then you can't program. – ADyson Sep 24 '21 at 12:24
  • All i asked it over here only coz its the best way to get things resolved and am not looking over here coz its just free. Ofcourse i can offer something from my behalf as appreciation but i work on a very small scale and don't have much but still i would love to. Sorry for the behaviour again. – Dvsh Sep 24 '21 at 12:24
  • `was working fine some time ago and now it stopped`...so then work out what you changed in between, and that should give you a clue. You have source control history, I hope? – ADyson Sep 24 '21 at 12:24
  • You might also want to read [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – ADyson Sep 24 '21 at 12:25
  • `Ofcourse i can offer something from my behalf as appreciation`...that's not what I meant. We volunteer for free here, no problem, but we only answer [on-topic](https://stackoverflow.com/help/on-topic) questions. Yours is currently off-topic because it's asking for debugging help but it doesn't contain a specific problem or error, or a clear description of the expected behaviour of the code for any given input values. You can edit the question to correct these issues, and then we can answer it. P.S. I already gave you a big clue anyway...see earlier comment about array_fill() – ADyson Sep 24 '21 at 12:26
  • `I don't understand this rand code`...what don't you understand specifically? Did you read the manual for the function? How can you write code using it if you don't understand what it's going to do? That's crazy. But as I said before - I don't think you need it because, according to your examples, there will only ever be 1 URL to choose from anyway, so you don't need to worry about picking one. – ADyson Sep 24 '21 at 12:31
  • Thank you so much! now i understood what was going wrong. i was missing the weightage part over there that's why it was not working. The correct code now i used which is working fine is if ($tym >= "06:20" && $tym < "06:30"){ $urls = array( array('url' => 'https://www.google.com/', 'weight' => 100) );} Thank you so much for the help in understanding. – Dvsh Sep 24 '21 at 13:01
  • The code you've shown doesn't try to use a "weight" property from your data, but...ok, if you say so. Maybe you're working on a different version that what you showed above. – ADyson Sep 24 '21 at 13:03
  • P.S. If you found the answer then you should write it in the Answers section below, for the benefit of others. If you put it only in the comments then it cannot be searched, or voted on, etc. You're allowed to answer your own question - see https://stackoverflow.com/help/self-answer. – ADyson Sep 24 '21 at 13:04

1 Answers1

1

I have corrected a few things and now it's all working. Here's the correct part:

date_default_timezone_set('Asia/Kolkata');      //your default timezone is utc
$tym = date("h:i");

if ($tym >= "06:00" && $tym < "07:00"){         //every hour values can be added similarly
$urls = array(
    array('url' => 'https://www.google.com/', 'weight' => 100)      //url to specify
);}


$probs = array();
foreach ($urls as $i => $url) {
    $probs = array_merge($probs, array_fill(1, $url['weight'], $i));
}

$rand = array_rand($probs);
var_dump($rand);
var_dump($urls[$probs[$rand]]['url']);

header('Location: '.$urls[$probs[$rand]]['url']);
Veeresh Devireddy
  • 1,057
  • 12
  • 24
Dvsh
  • 19
  • 4
  • 1
    Great. But I'd point out again that since `$urls` only contains 1 item, there's no point in having any code which tries to randomly select an item from it...guess what the result will always be! – ADyson Sep 24 '21 at 13:11