2

I have a raspberry pi which opens up a webpage on boot. This page loads includes a php page that listens on udp port for input. When the input is received I want another page to load, but I'm having trouble doing this.

Here's index.html

<html style="background-color:#0085b3;">
<body>
  <img src="bg.jpg"/>
  <div id="content"></div>
</body>

<script src="scripts/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#content").load("udp.php");
});
</script>
</html>

and udp.php

<?php

session_start();

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '10.10.10.25', 5000);

$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);

$buf = preg_replace('/\s+/', '', $buf);

$conn = null;

socket_close($socket);

die("<script>location.href = 'http://10.10.10.20/main.php'</script>");
?>

What happens is that the die function is echoed on the raspberry and the browser doesn't load the main.php webpage

I tried using a single php page to display the image and then listen to the input, but the php code is run at once and displays the html only after receiving some input.

index.php

<?php

echo '<html style="background-color:#0085b3;">';
echo '<body>';
echo '<img src="bg.jpg"/>';
echo '</body>';
echo '</html>';

session_start();

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '10.10.10.25', 5000);

$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);

$buf = preg_replace('/\s+/', '', $buf);

$conn = null;

socket_close($socket);

die("<script>location.href = 'http://localhost/plc/main.php'</script>");
?>
3
  • Why are you redirecting like that? why not use header(location: <URL>); die();? Commented Feb 22, 2016 at 12:18
  • use sleep() method i think i will work Commented Feb 22, 2016 at 12:21
  • PHP code runs always first and has to complete, there's no way around it -- stackoverflow.com/questions/10877399/… Commented Feb 22, 2016 at 12:44

1 Answer 1

3

PHP will execute first, then HTML and finally javascript.

  • You send request to server, server executes your script.
  • Then returns rendered html to browser, browser parses HTML (inline javascript executed).
  • Finally executes external included javascript files, one by one in order they are included.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.