Record User Actions Using PHP and Javascript - php

I am developing an online learning system (PHP, MySQL and Javascript). I would like to track what pages and how long each users spent on each page. Ideally, I would like to record this in a MySQL database. My question is 2 fold:
1. What kind of fields would I include in my db table to record multiple pages accessed?
2. Is this problem best approached by server side only or by using javascript ? e.g. server side: hidden form fields with a page id attached, page id is passed to db and recorded?or Javascript: record all actions in Javascript variables and somehow pass to db at end of session?
Really I am just looking for some high level guidance on an approach as opposed to code snippets.
GF

PHP isn't my normal language, but I would think about creating a module of code that can be called from the top of each of your scripts, that basically logs away "I served this page, with these form variables, at xxx ... ". To be more precise, I would record that in a table.
If you need to know when the user left your page, for a page on another site or perhaps shut down their browser for instance, then a purely server side solution isn't going to cut the mustard. In that event, you are going to have to start thinking about JavaScript, and intercepting events - such as the onUnload event...
Have a read here...

While I know of no solution that can track individual users out of the box (I'm sure there are some), I am pretty sure you could customize Piwik to do this. Piwik aims to be a self-hostable alternative to Google Analytics. It is open source and build on Zend Framework and MySql.
Piwik collects usage statistics through a JavaScript tracking code and a Webbug image for fallback. Basically, what you would need to do is pass the logged in user's user id to the tracking script and then write a plugin that knows how to handle this information.

Related

Jquery + PHP, wrapped in Phonegap, how to keep users logged in?

I'm making a simple mobile web app where users should be able to login/authenticate to sign up for private events.
I love the way JQuery mobile looks and feels, but want PHP to do everything in the backend: Handle form processing, talk to the DB and so on.
I currently have no authentication, but want to add that.
Wrapping the current solution in Phonegap actually worked, but I suspect the authentication/session handling is going to crash with that.
My hope is that I can exchange data with a PHP script when:
- the form loads (to check for PHP user session etc), and
- a "submit" button is clicked.
I covered the submit button part, and it works. Data is sent to, and returned from my PHP processing script to the JQuery front-end.
For handling the submit event, for some reason this code worked:
$(document).ready(function(){
as opposed to an onclick thing which didn't work.
This baffles me, as I read that document.ready is an event that occurs when the document has completed loading.
Q1: Could this have something to do with me using Jquery mobile sections (
Q2: What would be the correct JQuery event/trigger to use prior to loading, to control form "context"? (based on data from backend PHP).
Q3a: Is it a viable solution to rely on PHP sessions in such a case?
Q4: Also considering that I'd like to Phonegap it...will a PHP session be able to "see" the Jquery-based, phonegap-wrapped app like a client session just like if it was someone using a web-browser?
4b. When does a Phonegap app user session "end"? (When does the user have to log in again...).
I know similar questions have been raised several times, but please consider I'm a gullible idiot that knows too little to understand which case is similar enough to mine. There are some concepts here that I need to grasp before I can decide what to investigate and ask about :-)
Here's to tolerance and understanding! Cheers!
Eivind
You can store mobile id and users id in database with flag logged_in when they first logged in. Then again if user open that app, you can check the mobile id with the record in database whether user is exists or not and if exists then whats his/her status.

How to avoid some SQL queries in PHP?

I am developing a PHP web app with jQuery and Twitter Bootstrap. And it uses AJAX for everything. So, I show a form in HTML5, the user press a button (class="btn"), the form is sent to PHP (jQuery, AJAX), PHP makes a query to the MySQL and echoes an answer, which is shown in the form (jQuery). This is basically how the web app works.
But here's the deal, the first form it is showed, it's a div that shows some news. For example:
A new user was created.
There is new important date.
Someone wants to text you.
So I've created a table in MySQL called News where I saved some values than mean something like:
1: A new user was created
...
Everytime the user log in will se that. It means that there will be a query and a response as soon as the HTML5 get loaded when a user log in.
The index.html file has a navbar (Bootstrap), and a option call News. When the user clicks it, the same query will be executed, but not necessarily the same response.
I thought in modifying the div with news whenever the user does an action. But, an action can also be done by another user. So it is necessary to make the query again!
Is there any solution that allows me to avoid querying the database when the user wants to get the news? Or how can I know that it is necessary to update the div right now? I was taking a look at caching queries but didn't arrive to a conclution.
Sorry if my english is not too good, it is not my native language.
Thank you.
You can send a timestamp in every news response from the server and save it in javascript. The next time you make a request, send the timestamp you saved and the server checks if there are more recent news, sending nothing if there is none as the last response is still the newest.
Well, there is a downside here, you still need to make a query to the database (filtring the results with a WHERE clause like 'WHERE ... TIMESTAMP > last_timestamp_from_browser') which is perfectly valid, SGBDs are designed for this, and if you don't have thousands of users accessing your website at the same time there will not be any problem. With this approach you will only save bandwitdh as the connection to the database is still made.
There is another way that prevents this connection from being made, cache some values of last news inserted which could be user specific or global and save them in APC module (or memcached). You'll need to discover what to cache and when (you can't cache the entire database, just some well organized timestamps and maybe the most requested news for example). This way you prevent the database connection from being made. This will force you to do many many more code, so, use it only if you really need it, like thousands of user connections at once.

Extracting Data from Another Domain, possible?

I'm terrible at keeping track of my bills, so I wanted to create something automated. I also wanted the challenge of making it myself.
My questions:
Is it possible to have a webpage connect to another domain (any utility website i.e. timewarnercable.com) with the proper login credentials and retrieve the dollar amount I owe, then send me an email or even just display it on the webpage?
I've already got a webpage setup that has all my account info stored in it (don't worry it's only a local site!) and I can click a button and the info I have stored sends a POST request to the utility login site. This logs me in to my account page and then I can view the bill. But don't want it to open another page..I'd rather load the content of that page in the background, scan for the code where its says my $ owed, then capture that somehow, then return the dollar amount onto the webpage.
If so, is this possible to design this with Ruby (Rails) or php, with Javascript / AJAX.
Thanks!
What you're basically asking about is "page scraping", but your scenario is more complicated. You would have to fake the login post, capture and store any cookie/session info returned to you in the response and use that in subsequent requests to the site. You may also have to deal with redirects, depending on the site.
I have found nodejs actually quite useful for scraping pages since it has plugins that provide dom selectors (there is a jquery plugin) - you're using javascript for server-side programming.
Check if the site has API and if the site provides that, will make your life a ton easier.
Some banks like BankOfAmerica have applications that already do this - they aggregate your accounts and bills from other sites, see if your bank can do this.

back button in ajax supported pages

i have a master php page and now i am loading different pages in a div of this master page using ajax. now i want to add a back button on the slave pages so that the div can loaded with its just previous slave page but the back button, i am using uses history.go(-1) that take me away from master page. has anybody any solution. Thanks in advance.
the code i am using is written below. but it is not working in ajax pages
Go back
My suggestions would be to use a framework for this purpose like backbone.js
Where you can setup routes just like on server side.
I feel this is the best way to do this on ajax sites.
Read tutorial on Manipulating browser history.
It mentions:
pushState, which is used for creating "entries" in the browser's history (with attached data about state), allowing to get back to some specific state you previously saved,
onpopstate event, that is fired when you go back or forth through the browser's history, and passes information previously saved for specific entry in the history (so you actually can read data previously saved by pushState for this specific entry),
In case pushState and onpopstate do not work for you, there is still some ability to work on the basis of onhashchange event (however limited).
The good news is that what you want to achieve is definitely possible. It is even implemented in many famous sites such as Twitter, GitHub, Facebook etc.
You can use the history functionality provided in newer browser. There is several libs that already provide functionality for you:
For JavaScript MVC applications:
http://documentcloud.github.com/backbone/#History
For mobile web pages:
http://jquerymobile.com/demos/1.0b1/#/demos/1.0b1/docs/pages/docs-navmodel.html
For implementing a solution your self:
http://benalman.com/projects/jquery-hashchange-plugin/

Auto populate form fields on external sites from local MySQL

Howdy folks, I am wanting to build a script or something to take a single row from my MySQL database and use that data to pre-populate form fields on one of multiple sites that aren't mine. What I'd like to do is to take information a user has entered on my site and when they click a link to one of the sites in my system it loads the external site with certain pre-mapped fields populated with the info they entered. But I can't seem to get my head around a way to do this, seeing as I can't add anything to these pages. Do you guys have any suggestions?
The flow you described is not possible due to cross-site scripting constraints. This post is relevant: Browser Automation and Cross Site Scripting
The closest thing I can think of is Greasemonkey, which would force the user to download the plugin from Mozilla, plus a new userscript from your website.
Another option would be reproducing the form on your own web server, and hoping the form action doesn't perform referrer checks.
i am not very sure but you can use wget and pass xml data...i.e you can build an xml string with the data you want to send across and then do a wget to the other site...hope this helps

Categories