YvetteLee
YvetteLee

Reputation: 1090

php - load two pages one after another

i have one page with a navbar button on top. Also, i use one sonoff wifi switch to power on/off one lamp.

I am trying to make this task order to work.

  1. User press on navbar the button.
  2. Load power_off.php, which is the code below.
  3. After 2seconds redirect to google page.

Code power_off.php

<?php
session_start();
unset($_SESSION['valid']);
unset($_SESSION['timeout']);

header( "url=http://192.168.2.2/cs?cmnd=Power%20Off" );
header( "refresh:2;url=http://www.google.gr/" );
?>

Thank you.

Upvotes: 2

Views: 150

Answers (1)

Solution modifying the Power Off script:

In your entry script:

<?php
session_start();
unset($_SESSION['valid']);
unset($_SESSION['timeout']);

header( "Location: http://192.168.2.2/cs?cmnd=Power%20Off" );
?>

then at the end of that file, the one that makes the poweroff you add

sleep(2); //if you want to wait 2 seconds... 
header( "Location: http://www.google.gr/" );
exit;

Solution with CURL:

<?php
session_start();
unset($_SESSION['valid']);
unset($_SESSION['timeout']);

//Initialize cURL.
$ch = curl_init("http://192.168.2.2/cs?cmnd=Power%20Off"); 
//Execute the request.
curl_exec($ch); 
//Close the cURL handle.
curl_close($ch);

sleep(2); //wait for 2 seconds. Remove if not needed.
header( "Location: https://www.google.com" ); //redirect to Google
exit;
?>

Upvotes: 4

Related Questions