8

I developed a website for my graduation however it still only one thing I have do. What I want is when the script is installed on a website I want to send the name of the website who has installed my script, also whenever there is an error I want to send it to my website so for example:

This website installed my script

www.security-dz.com/myscript

I want to see the path + website in an other file in other website. For example:

www.getlog.com/mylogs.php

The purpose of this is keep my customers update and give them support and see the errors that happen so I can fix them in next updates.

Jenz
  • 8,280
  • 7
  • 44
  • 77
user3395024
  • 153
  • 2
  • 9
  • 1
    this is a good idea :) i hope they answer it – Walid Naceri Mar 29 '14 at 11:02
  • 2
    What have you tried? The first part (without errors) is pretty simple, just make a request to send the address of the website to you when the script is installed. – XCS Mar 29 '14 at 11:06
  • well the user will install it manually that's why i want to make the request in the index page, the problem is i don't know how to send how i make the request or variable to be send automatically – user3395024 Mar 29 '14 at 11:10
  • you will be storing those details in a database right? – anurupr Mar 29 '14 at 11:12
  • @anurupr yes for example i send a varible $_GET['path'] = the_path_of the website to my website and store it in the database – user3395024 Mar 29 '14 at 11:15
  • you can create a `http` request to a form on your website : http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php – A.Essam Mar 29 '14 at 12:30
  • Can you add more details like, how does the user install the application on their site? If the run the script then you can just make an HTTP request using the method mentioned in the comment above. – asp Mar 29 '14 at 19:45
  • First You should have search for Cross Domain Ajax. – Ritesh Chandora Mar 31 '14 at 10:05
  • @YehiaSedky Wouldn't that send a request to the server that served the included script with error reporting? The error happens at client-side. I don't quite understand your comment. @ user3395024 Are you getting there? – dot slash hack Apr 01 '14 at 14:57
  • @KemHeyndels you are right if it is a Javascript script. But I understood it is a PHP script. Please clarify @ user3395024 – A.Essam Apr 01 '14 at 15:41

3 Answers3

2

You might want to take a closer look at the JQuery docs for ajax requests, so you can use a secure http connection for logging. This javascript code basically describes a function that sends the errors in text-format to your server-side script. This script can in turn write the error description to a file on the server. I'd recommend using a DB instead; That way you can easily write a web-client that displays all reported errors (and filters and the other good stuff).

You can extract the origin url from the referer [sic] field in the ajax http get-request on the server.

(function () { // function operator, in case console doesn't exist
    !console ?
        (console = {}) : console;
    !console.log ?
        (console.log = function () { }) : console.log;
    !console.info ?
        (console.info = console.log) : console.info;
    !console.error ?
        (console.error = console.log) : console.error;
}());
// Uses JQuery
function reportError (errDesc) {
    var path = "www.getlog.com/mylogs.php";
    $.ajax({
        url: path,
        type: "GET",
        async: true,
        cache: false,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        crossDomain: true,
        data: errDesc,
        dataType: "jsonp",
        error: function (req, type, errObj) {
            console.error("Reporting error failed: " + type + "\nAt url: " + path + "\n" + errObj);
        // In case you need to debug the error reporting function
        },
        succes: function (res) {
            console.info("Reported error to server:\nRequest:" + errDesc + "\nResponse: " + res);
        // extra error logging facility on client-side, invisible to most users
        },
        global: false // prevent triggering global ajax event handlers
    });
    return errDesc; // in case you want to reuse the errDesc
}

Code has been validated with jshint. Please let me know if there are still issues, because I didn't take the time to completely replicate your setup (setting up 2 different domains etc.)

Addendum: Some useful reading if you're having issues with cross-domain messaging, JSON is not a subset of javascript, Cross-origin resource sharing, JSONP.

dot slash hack
  • 558
  • 1
  • 5
  • 13
  • That information is incorrect, modern browser versions support both console.error and console.info: [opera](http://www.opera.com/dragonfly/documentation/console/), [IE](http://msdn.microsoft.com/en-us/library/hh772183%28v=vs.85%29.aspx), [Chrome](https://developers.google.com/chrome-developer-tools/docs/console). Edited code to use polyfill or stub if console is not available. – dot slash hack Mar 31 '14 at 09:35
  • Cross Domain Ajax Error. – Ritesh Chandora Mar 31 '14 at 10:04
  • @RiteshChandora Forgot to set datatype to jsonp, thanks for checking. – dot slash hack Mar 31 '14 at 10:14
  • My bad, This code will raise errors on 75% of my clients browsers. I'd leave out console.error and console.info on production release. – Joeri Mar 31 '14 at 11:34
  • @Kem Heyndels: I know what polyfill is for. What makes you think users have their inspector/console open? – Joeri Apr 03 '14 at 11:53
0

What you could do is post both the name of the website that uses your script and the error code variable with AJAX through URL to your logging website, which will then get the variable and name from the URL and use these to add to your log.

You should, however, by using this tactic, also make use of some URL validation, otherwise this will leave you wide open to injection attacks.

Max Langerak
  • 1,187
  • 8
  • 17
0

it is easy when your script installed , get the site info and send by socket and http request with get method and then recive it on your server.

for errors, php has some method to control error logs so custom it.

RayanFar
  • 539
  • 11
  • 28