This is killing me.
I am building a mobile app using apache cordova where jQuery AJAX and PHP are being used to communicate with the server and database. I first discovered this issue when I was encountering errors that my teammates were not, and we were using the same code. In some of our PHP code, we were using $_SESSION variables to store data but it is not being stored properly when running the app on my machine. Here is an example of the code that is not working:
<?php
header('Access-Control-Allow-Origin : *');
include 'connection.php';
$userId = $_SESSION["userId"];
$query = "SELECT * ".
"FROM `db`.`users` ".
"WHERE `userId` = $userId;";
$result = $conn->query($query);
$result_array = array();
while($row = mysqli_fetch_assoc($result))
{
$result_array[] = $row;
}
echo json_encode($result_array);
In a page that runs prior to this, a different php file is called containing this line:
$_SESSION["userId"] = $userId;
include connection.php contains all our user/login info as well as session_start(); before anything. The workaround was this: This error does not happen when I build the app and test it on a device. It only occurs on my machine when debugging the app through the ripple emulator in a chrome browser window. Because I did not want to create a new build so frequently, I changed our code to store the data on the front end, using sessionStorage, and had to disable the Cross Domain Proxy setting on the emulator.
This is where I believe the error is: None of my AJAX calls are successful unless I set the Cross Domain Proxy to "Disabled", but if I do that, PHP $_SESSION no longer works (I discovered the session id just resets every new call). My teammate can debug on the emulator just fine and his Cross Domain Proxy is set to "Local". When I try it this way, all of my AJAX calls error out once again.
Also - one last thing to note: When I run the app in just a web browser, not using the ripple emulator or anything, everything runs just fine. Which would narrow the issue down to something specifically with the ripple emulator.
I am relatively new to this stuff - CORS and AJAX requests, but I did some serious research before posting on here - this is my last resort. I will be monitoring this question frequently in case you need anything else from me to help me solve this issue. Thanks!
EDIT: Below is the error I get when I run ripple emulate from the command line after making some of the suggested changes, it's the same error I was getting before:
C:\Users\Brian\Documents\HoH Docs\Source Code\gitDev\HoH\www>ripple emulate
INFO: Server instance running on: http://localhost:4400
INFO: CORS XHR proxy service on: http://localhost:4400/ripple/xhr_proxy
INFO: JSONP XHR proxy service on: http://localhost:4400/ripple/jsonp_xhr_proxy
INFO: Could not find cordova as a local module. Expecting to find it installed g
lobally.
INFO: Using Browser User Agent (String)
INFO: Proxying cross origin XMLHttpRequest - http://www.server.com/php/
signIn.php?email=b_dz#gmail.com&password=test
_http_outgoing.js:347
throw new TypeError(
^
TypeError: Trailer name must be a valid HTTP Token ["access-control-allow-origin
"]
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:347:13)
at ServerResponse.res.setHeader (C:\Users\Brian\AppData\Roaming\npm\node_mod
ules\ripple-emulator\node_modules\express\node_modules\connect\lib\patch.js:59:2
2)
at Request.pipeDest (C:\Users\Brian\AppData\Roaming\npm\node_modules\ripple-
emulator\node_modules\request\main.js:723:12)
at C:\Users\Brian\AppData\Roaming\npm\node_modules\ripple-emulator\node_modu
les\request\main.js:614:14
at Array.forEach (native)
at ClientRequest.<anonymous> (C:\Users\Brian\AppData\Roaming\npm\node_module
s\ripple-emulator\node_modules\request\main.js:613:18)
at ClientRequest.g (events.js:260:16)
at emitOne (events.js:77:13)
at ClientRequest.emit (events.js:169:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:433:21
)
And get this from the Fiddler Web Debugger when the request is made:
The connection to 'localhost' failed. <br />Error: ConnectionRefused (0x274d). <br />System.Net.Sockets.SocketException No connection could be made because the target machine actively refused it 127.0.0.1:4400
This is most likely your problem:
header('Access-Control-Allow-Origin : *');
include 'connection.php';
You are sending headers to the browser before you include the connection file where you start the session.
On some testing environments, output buffering might be turned on so this would work. However, on your environment it seems it is not so your session_start() in connection.php will fail.
You should not output anything to the browser before the call to session_start() so in this case you should move the header() call to below the include:
include 'connection.php';
header('Access-Control-Allow-Origin : *');
You can probably verify that this is the problem in the server error log.
You might be have to set header in your ajax request
if your are using jQuery ajax method then add below option
xhrFields: {
withCredentials: true
}
Or if you send ajax request using XMLHttpRequest object then it should be something like belwo
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);
XmlHttpRequest responses from a different domain cannot set cookie
values for their own domain unless withCredentials is set to true
before making the request, regardless of Access-Control- header
values.
Related
I have a Wordpress website with a working order system. Now I want to make an Android app which displays every new order in a list view as soon as the order was made.
The last two days I thought about the following solutions:
Simple HTTP GET requests every 10 seconds
Websockets
MySQL binary log + Pusher Link
Server Sent Events
My thoughts (working with a LAMP stack):
Simple HTTP requests are obviously the most ineffective solution.
I figured out that websockets and Apache aren't working well together.
Feels quite hacky and I want to avoid any 3rd party service if I can.
4. Looks like this is the optimal way for me, however there are some problems with Apache/php and Server Sent Events from what I experienced.
I tried to implement a simple demo script but I don't understand why some of them are using an infinite while loop to keep the connection open and others don't.
Here is an example without a loop and here with an infinite loop, also here
In addition to that, when I tested the variant with the infinite loop, my whole page won't load because of that sleep() function. It looks like the whole server freezes whenever I use it.
Does anyone have an idea how to fix that? Or do you have other suggestions?
That is the code that causes trouble (copied from here) and added a missing curly bracket:
<?php
// make session read-only
session_start();
session_write_close();
// disable default disconnect checks
ignore_user_abort(true);
// set headers for stream
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
// Is this a new stream or an existing one?
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0) {
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}
echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
echo "retry: 2000\n";
// start stream
while(true){
if(connection_aborted()){
exit();
}
else{
// here you will want to get the latest event id you have created on the server, but for now we will increment and force an update
$latestEventId = $lastEventId+1;
if($lastEventId < $latestEventId){
echo "id: " . $latestEventId . "\n";
echo "data: Howdy (".$latestEventId.") \n\n";
$lastEventId = $latestEventId;
ob_flush();
flush();
}
else{
// no new data to send
echo ": heartbeat\n\n";
ob_flush();
flush();
}
}
// 2 second sleep then carry on
sleep(2);
}
?>
I'm thankful for every advice I can get! :)
EDIT:
The main idea is to frequently check my MySQL database for new entries and if there is a new order present, format the data nicely and send the information over SSE to my android application.
I already found libraries to receive SSEs on android, the main problem is on the server side.
Based on your question I think you could implement SSE - Server sent events, which is part of HTML5 standard. It is a one-way communication from server to client. It needs html/javascript and a backend language, e.g PHP.
The client will subscribe on events and when subscription is up and running the server will send any updates from the input data. As standard the update will be visible each 3 seconds. This can be adjusted though.
I would recommend you to first create a basic functioning web-browser-client as a start. When and if it is working as you expect, only then you would judge about the effort of building the client as an app.
You would probably need to add functions on the client-side, such as start/stop the subscription.
My understanding of users not recommending the combination of (server sent events) and Apache is the lack of control how many open connections there are and what would control the continuously need of closing of connections. This could lead to sever server performance problems.
Seems using for example node.js would not cause that problem.
Here are some start link:
MDN:
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Stream Updates with Server-Sent Events:
https://www.html5rocks.com/en/tutorials/eventsource/basics/
I am using Symfony's Custom Session handler. It works fine when I set the session for the first time but when I refresh the page. The page will keep on loading and after 30 seconds which is max_execution_time defined in php.ini, the errors are shown. To setup custom save handler, I created a table sessions in ratchet database. The structure and the data in the table:
Now the script, on the top of page looks like this to initialize custom save handler:
<?php
ini_set('session.auto_start', false);
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
require dirname(__DIR__) . '/vendor/autoload.php';
$pdo = new \PDO('mysql:host=localhost;dbname=ratchet', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$storage = new NativeSessionStorage(array(), new PdoSessionHandler($pdo)); // Here when PdoSessionHandler class is initialized again on refreshing it locks the page there
$session = new Session($storage);
$session->start();
$username = $session->get('username');
The errors thrown by PHP script for Line 153:
Line 153 is a statement in the following method which executes and reads a session value from database.
private function doRead($sessionId)
{
$this->sessionExpired = false;
if (self::LOCK_ADVISORY === $this->lockMode) {
$this->unlockStatements[] = $this->doAdvisoryLock($sessionId);
}
$selectSql = $this->getSelectSql();
$selectStmt = $this->pdo->prepare($selectSql);
$selectStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
$selectStmt->execute(); // Line 153
Is something broken in the framework's component or am I missing something? I have followed the documentation. I can't find anything that is new or has been added by myself.
Addition
In addition to initializing session in the web app, I initialize symfony session in my server script as well because it is required in order to attach the same session object to each connection. The points to be noted:
The application works fine for the first attempt.
Chat server works as intended. It never throws an error.
When the page in which I connect to the websocket server is refreshed, it remains in the loading state for 30 seconds. Then the errors occur.
Same happens in other browsers. Like it will let the connection establish but when I will refresh ........ See above :D
You mentioned that:
I initialize symfony session in my server script as well
Does this also call new NativeSessionStorage?
What I think is happening is that you are creating essentially two session handlers but PHP only knows about one and thus only closes the database connection / lifts the database lock for one of them. Let me see if I can explain clearly. Every time you instantiate NativeSessionStorage the class registers the sessionHandler (the pdo session handler in your case) with php via session_set_save_handler which you can see in the NativeSessionStorage code here.
When PHP stops execution it calls the PdoSessionHandler->close() method. However since you have two instances and php only has one registered as the session handler, its closing one but not the other. Which keeps the database locked. Based on your description I think this may be your problem. You should be able to easily test this by explicitly calling Session->save() try doing this explicitly in your app and server code to see if you don't get the locked error anymore.
Hope this helps!
Is the session closing when the websocket disconnects? If not, it is keeping the lock on the session record and not allowing the incoming request to continue past starting the session. The initial page load works because the websocket picks up the session when the page load is complete.
I'm using in my project server-sent events where the JS is calling a PHP page, say eventserver.php which consists basically of an infinite loop which checks the existence of an event in a $_SESSION variable.
On my first implementation this lead my website to hung up because the eventserver took the lock on the session and did not release it until the timeout expired; however, I managed to resolve this issue by locking/unlocking the session by using session_write_lock() and
session_start() continuously in the loop.
This is actually causing a lot of PHP warnings (on Apache error.log) saying that "cannot send session cache limiter - headers already sent", "cannot send session cookies" and so on.
Posting some code here
session_start();
header('Cache-Control: no-cache');
header('Content-Type: text/event-stream');
class EventServer
{
public function WaitForEvents( $eventType )
{
// ... do stuff
while( true )
{
// lock the session to this instance
session_start();
// ...check/output the event
ob_flush();
flush();
// unlock the session
session_write_close();
sleep( 1 );
}
}
}
Why is this happening?
I am doing the same thing as the OP and ran into the same issue. Some of these answers don't understand how eventSource should work. My code is identical to yours and uses a session variable to know what view the user is on which drives what data to return in the event of a server trigger. It's part of a realtime collaboration app.
I simply prepended an # to the session_start() to suppress the warnings in the log. Not really a fix, but it keeps the log from filling up.
Alternatively, not sure how well it would work for your application, but you could use ajax to write the session variable you are monitoring to the database, then your eventSource script can monitor for a change in the DB instead of having to start sessions.
This is not a good idea. HTTP is a request-response protocol so if you want server-client communication to be bi-directional you will need to look into websockets or something similar. There are also things like "long polling" and "heart beating"
If you want an event loop try something like servlets in apache tomcat.
You will grapple for hours with issues because of your design.
Also check out ajax if you just want to shoot messages from javascript to PHP.
Make sure you know an overview of the tech stack you are working with :)
You don't need an infinite loop with SSE. The EventSource keeps an open connection to the server and any update on the server side data will be read by the client.
Check out basic usage of SSE here
It's probably because you start the session twice in your code. Don't restart the session at the beginning of the loop, but after the sleep().
I have a simple script that makes redirection to mobile version of a website if it finds that user is browsing on mobile phone. It uses Tera-WURFL webservice to acomplish that and it will be placed on other hosting than Tera-WURFL itself. I want to protect it, in case of Tera-WURFL hosting downtime. In other words, if my script takes more than a second to run, then stop executing it and just redirect to regular website. How to do it effectively (so that the CPU would not be overly burdened by the script)?
EDIT: It looks that TeraWurflRemoteClient class have a timeout property. Read below. Now I need to find how to include it in my script, so that it would redirect to regular website in case of this timeout.
Here is the script:
// Instantiate a new TeraWurflRemoteClient object
$wurflObj = new TeraWurflRemoteClient('http://my-Tera-WURFL-install.pl/webservicep.php');
// Define which capabilities you want to test for. Full list: http://wurfl.sourceforge.net/help_doc.php#product_info
$capabilities = array("product_info");
// Define the response format (XML or JSON)
$data_format = TeraWurflRemoteClient::$FORMAT_JSON;
// Call the remote service (the first parameter is the User Agent - leave it as null to let TeraWurflRemoteClient find the user agent from the server global variable)
$wurflObj->getCapabilitiesFromAgent(null, $capabilities, $data_format);
// Use the results to serve the appropriate interface
if ($wurflObj->getDeviceCapability("is_tablet") || !$wurflObj->getDeviceCapability("is_wireless_device") || $_GET["ver"]=="desktop") {
header('Location: http://website.pl/'); //default index file
} else {
header('Location: http://m.website.pl/'); //where to go
}
?>
And here is source of TeraWurflRemoteClient.php that is being included. It has optional timeout argument as mentioned in documentation:
// The timeout in seconds to wait for the server to respond before giving up
$timeout = 1;
TeraWurflRemoteClient class have a timeout property. And it is 1 second by default, as I see in documentation.
So, this script won't be executed longer than a second.
Try achieving this by setting a very short timeout on the HTTP request to TeraWurfl inside their class, so that if the response doesn't come back in like 2-3 secs, consider the check to be false and show the full website.
The place to look for setting a shorter timeout might vary depending on the transport you use to make your HTTP request. Like in Curl you can set the timeout for the HTTP request.
After this do reset your HTTP request timeout back to what it was so that you don't affect any other code.
Also I found this while researching on it, you might want to give it a read, though I would say stay away from forking unless you are very well aware of how things work.
And just now Adelf posted that TeraWurflRemoteClient class has a timeout of 1 sec by default, so that solves your problem but I will post my answer anyway.
I have noticed a few websites such as hypem.com show a "You didnt get served" error message when the site is busy rather than just letting people wait, time out or refresh; aggravating what is probably a server load issue.
We are too loaded to process your request. Please click "back" in your
browser and try what you were doing again.
How is this achieved before the server becomes overloaded? It sounds like a really neat way to manage user expectation if a site happens to get overloaded whilst also giving the site time to recover.
Another options is this:
$load = sys_getloadavg();
if ($load[0] > 80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
I got it from php's site http://php.net/sys_getloadavg, altough I'm not sure what the values represent that the sys_getloadavg returns
You could simply create a 500.html file and have your webserver use that whenever a 50x error is thrown.
I.e. in your apache config:
ErrorDocument 500 /errors/500.html
Or use a php shutdown function to check if the request timeout (which defaults to 30s) has been reached and if so - redirect/render something static (so that rendering the error itself cannot cause problems).
Note that most sites where you'll see a "This site is taking too long to respond" message are effectively generating that message with javascript.
This may be to do with the database connection timing out, but that assumes that your server has a bigger DB load than CPU load when times get tough. If this is the case, you can make your DB connector show the message if no connection happens for 1 second.
You could also use a quick query to the logs table to find out how many hits/second there are and automatically not respond to any more after a certain point in order to preserve QOS for the others. In this case, you would have to set that level manually, based on server logs. An alternative method can be seen here in the Drupal throttle module.
Another alternative would be to use the Apache status page to get information on how many child processes are free and to throttle id there are none left as per #giltotherescue's answer to this question.
You can restrict the maximum connection in apache configuration too...
Refer
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#maxclients
http://www.howtoforge.com/configuring_apache_for_maximum_performance
This is not a strictly PHP solution, but you could do like Twitter, i.e.:
serve a mostly static HTML and Javascript app from a CDN or another server of yours
the calls to the actual heavy work server-side (PHP in your case) functions/APIs are actually done in AJAX from one of your static JS files
so you can set a timeout on your AJAX calls and return a "Seems like loading tweets may take longer than expected"-like notice.
You can use the php tick function to detect when a server isn't loading for a specified amount of time, then display an error messages. Basic usage:
<?php
$connection = false;
function checkConnection( $connectionWaitingTime = 3 )
{
// check connection & time
global $time,$connection;
if( ($t = (time() - $time)) >= $connectionWaitingTime && !$connection){
echo ("<p> Server not responding for <strong>$t</strong> seconds !! </p>");
die("Connection aborted");
}
}
register_tick_function("checkConnection");
$time = time();
declare (ticks=1)
{
require 'yourapp.php' // load your main app logic
$connection = true ;
}
The while(true) is just to simulate a loaded server.
To implement the script in your site, you need to remove the while statement and add your page logic E.G dispatch event or front controller action etc.
And the $connectionWaitingTime in the checkCOnnection function is set to timeout after 3 seconds, but you can change that to whatever you want