php - Using javascript to get server information - Stack Overflow

I'm trying to rewrite a snipped of PHP into JavaScript, ideally passing as few as possible variabl

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.

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.

Share Improve this question edited Jul 25, 2012 at 23:11 Lix 48k12 gold badges107 silver badges134 bronze badges asked Jul 25, 2012 at 23:00 LTMELTME 1,1261 gold badge10 silver badges13 bronze badges 1
  • Can you perhaps be more specific as to exactly what properties you are interested in? – Lix Commented Jul 25, 2012 at 23:09
Add a ment  | 

2 Answers 2

Reset to default 5

window.location can be read or set and es 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.
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).

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 .ajax() function to retrieve all the server data you'll need.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744248319a4565043.html

相关推荐

  • php - Using javascript to get server information - Stack Overflow

    I'm trying to rewrite a snipped of PHP into JavaScript, ideally passing as few as possible variabl

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信