56347

Using javascript to get server information

Question:

I'm trying to rewrite a snipped of PHP into JavaScript, ideally passing as few as possible variables between the two. Are there any super-global variables in JavaScript that give similar information to the PHP $_SERVER array?

In JavaScript, location seems to give me a bit of useful information but what about something like the PHP equivalent of $_SERVER['HTTP_REFERER']? I also have jQuery as a resource.

Answer1:

window.location can be read or set and comes with properties such as .pathname, .search, .protocol, et cetera -- each of which will provide that particular aspect of the URL as a read-only value.

document.referrer will provide the referring URL as a string. If you want the domain/path/query/et cetera to be separated from one another, you will need to do that yourself, or use a library which will provide it.

document.cookie will provide you a semicolon-delimited list of user/server-set cookies. Again, turning that into an array or an object is on your own shoulders.<br /> Your cookie string also has no access to expiration-times, nor applicable paths the particular cookie is set for -- security.

For most of the rest of the data, you're going to have to talk to the server -- the browser likes to keep client-side script in the dark about things (like the user's IP, or session-variables, or anything else which can be turned into a security-risk).

Answer2:

This will get you the referrer -

document.referrer

But I think you are looking at this in the wrong way - JavaScript and jQuery are run client side - if you want server variables accessible - you'll have to pass them to the script - possible by means of an AJAX call. You say that you have jQuery available - so you can use the <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">.ajax() function</a> to retrieve all the server data you'll need.

Recommend

  • Why are my PHP sessions dying? And why can't I restore them?
  • UTC Time, Timezones, Daylight Savings, and Daylight Savings switchover dates
  • Why is the output of print in python2 and python3 different with the same string?
  • Fastest way to get random php number?
  • Seahorse::Client::NetworkingError Amazon S3 file upload with rails
  • Why `Failed to execute 'pushState' on 'History'` when using `Link` from react-ro
  • SWIG ImportError: undefined symbol: _Py_RefTotal
  • How can I create a list of elements with the same xpath using selenium with python?
  • KineticJS : scaling issue while using custom clipFunc on 2x pixel aspect display
  • Alternate (Free) to AOP PostSharp for method tracing (and exception)
  • Displaying and sizing a grayscale from a QImage in Qt
  • PHP problems with current url
  • PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)
  • UIImagePickerControllerDelegate Methods Not Called When Delegate Not UIViewController
  • Base64 as method of sanitizing user input for Mysql
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • gspread or such: help me get cell coordinates (not value)
  • Zurb Foundation _global.scss meta styles for js?
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Why does access(2) check for real and not effective UID?
  • Google Custom Search with transparent background
  • Alert pop up with LWUIT
  • Q promise. Difference between .when and .then
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Insert into database using onclick function
  • What is Eclipse's Declaration View used for?
  • Join two tables and save into third-sql
  • Can I make an Android app that runs a web view in Chrome 39?
  • How to make Safari send if-modified-since header?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory