0

For marketing reasons, I want to personalize my website based on what ISP/service provider someone is using. Google Analytics/Optimize/GTM do not provide this out of box. ISP/host lookup can be done with PHP, but I need the value in javascript so I can push it to the data layer.

I am not a developer, I've been piecing together bits and pieces from PHP references, Javascript references, and Ajax references.

This is what I have in my (WordPress) header file before closes. My website doesn't crash when it loads, but the javascript variable is constantly empty.

What can I do to pass it as a javascript variable?

  <?php $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); ?>
    <script>var jsHostname = <?php $hostname?>;</script>

2 Answers2

0

You dont need php to get the hostname ,With only javascript you can get the hostname, using the following:

var host = location.hostname
Risa__B
  • 451
  • 4
  • 8
0

json_encode() deals with arrays, strings etc.

 <?php echo $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); ?>
<script> var jsHostname =<?php echo json_encode($hostname); ?>;</script> 
Pawan Thakur
  • 591
  • 8
  • 19