1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

storing javascript variable in database in php

Discussion in 'PHP' started by mi6hq, Mar 28, 2019.

  1. #1
    In php I am trying to get screen resolution of website visitor by 2 different javascript commands.

    $width = "<script>var windowWidth = screen.width; document.writeln(windowWidth); </script>";
    $height = "<script>var windowHeight = screen.height; document.writeln(windowHeight); </script>";
    SEMrush
    AND

    $width = "<script>document.write(screen.width);</script>";
    $height = "<script>document.write(screen.height);</script>";

    $screenres = $width." x ".$height;

    When I echo $screenres variable in php I get correct screen resolution but when I try to save this variable in sql database then "<script>d" is saved instead of actual variable of this variable.

    Any help?

    MI6
     
    Solved! View solution.
    mi6hq, Mar 28, 2019 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    25,186
    Likes Received:
    3,428
    Best Answers:
    100
    Trophy Points:
    665
    #2
    It's not clear how you're getting any information back to the server to save - is there a form somewhere?

    If you are using a form add a hidden input to it and use javascript to set the value.

    If it's not a form, then use an ajax call to send the information you need saved down to a php script on the server. Javascript can't save to your database.
     
    sarahk, Mar 28, 2019 IP
  3. mi6hq

    mi6hq Member

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks for replying.

    As I mentioned before I am trying to save website visitor data like os, browser, referral url, resolution, etc in sql database. Following php code works fine if I display resolution on page.

    <?
    
    // $width = "<script>var windowWidth = screen.width; document.writeln(windowWidth); </script>";
    // $height = "<script>var windowHeight = screen.height; document.writeln(windowHeight); </script>";
    
    $screenres = $width." x ".$height;
    
    echo $screenres; // It means screen resolution value "1366 x 768" in passed from javascript to php variable "$screenres".
    
    ?>
    PHP:
    But when I save this variable $screenres into sql database alongwith other data like os, browser, referral url then "<script>doc" is saved.

    Hope you understand..

    MI6
     
    mi6hq, Mar 28, 2019 IP
  4. #4
    Its working in browser because you are echoing the PHP variable, which is sending the javascript code to the browser.
    The browser is executing the js and you are seeing the output.
    Look at the source code of the page and you will understand...

    When you send the PHP var to db, then again its sending the same js code to the db, and it stores that code, instead of the js output.

    You need ajax to send js output to a PHP variable.

    Just find a js ajax function, and append height and width to a PHP script in url being called by the ajax.
    In the php script, GET the appended values and store in a database.
     
    JEET, Mar 29, 2019 IP
  5. mi6hq

    mi6hq Member

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thanks for explaining this to me, now I understand what's going on with this variable.

    Actually I already did this by appending variable value in url and then extracting value by GET method and it works fine.

    But after refresh sometimes "referrer url" is set to same php page. Since after processing data I have to forward visitor to external website so I don't want my php page url is shown as "referral url" in external site statistics. Any idea how to stop php setting "referral url" if screen resolution is get by this method?

    MI6
     
    mi6hq, Mar 29, 2019 IP
  6. JEET

    JEET Well-Known Member

    Messages:
    2,275
    Likes Received:
    118
    Best Answers:
    2
    Trophy Points:
    185
    #6
    The referrer is in "their" server log, I don't think you will be able to control it. However, if you wish to hide just this processing page URL, then first redirect to another URL on your website, which is not so unsafe to reveal, then from that page sent the user to third party website.

    Also, I don't know how you are referring, cause I think a header("location: third_party_url") will not set current page as referrer page...
     
    JEET, Mar 29, 2019 IP