Hi all,

How to obtain the value stored in the address bar to a string variable in php?

Thanx for any help in advance,

Regards,

tvks

    maybe using javascript

    this.location.href ?!

    but that will only give you the value of the location of the window that you are using

      Hi acey

      As I am new to PHP can u explain better.

      My intention is to have the value in a php variable like $address, which I can use to display also.

      echo $address.

      With regards,

      tvks

        You can't retrieve what's in the address bar.

        You CAN retrieve the current URL (location) using a combination of $SERVER['HTTP_HOST'], $SERVER['SCRIPT_NAME'] $SERVER['PATH_INFO'] and $SERVER['QUERY_STRING'], or other variables available to PHP.

        The actual method for doing this is fairly involved, you need to
        - Determine whether it's http or https
        - Get the host name from $SERVER['HTTP_HOST']
        - Get the URI path from a combination of $
        SERVER['SCRIPT_NAME'] and $SERVER['PATH_INFO']
        - Get the query string from $
        SERVER['QUERY_STRING']

        And stick all the pieces together correctly.

        Unfortunately, the fragment part of the URL (e.g. http://example.com/#fragment ) is not sent to the server, therefore you can't retrieve it.

        Mark

          Hi Mark,

          Assuming that I am not at the server but I want to get is what is currently in my address bar.

          The fact is that I want to have the fragment part much more than the server name or http. Is there no way out.

          Regards

          tvks

            Javascript is not allowed to access the current contents of the address bar, for security reasons.

            You can get the location of the current document in Javascript, but there are plenty of cases where this is not the same as the contents of the address bar.

            As I explained, you can (in PHP) determine the full URL, except for the fragment, which is never passed to the server.

            Mark

              tvks:

              I think there is a miscommunication going on here caused by a confusion of client side code and server side code.

              Some languages like PHP, Perl, ASP, CFM run on the server and they create the web page that you see. They build the page (often a mix of HTML and Javascript) and send the result over the Internet to the user.

              Once the page arrives at the destination, any client side code that exists in the page like Javascript or Flash can be executed. It can modify or alter the page once it has arrived.

              Since PHP runs server side, it has no access to the address bar but it does know the URL of the page.

              Since Javascript runs client side, it does have access to the address bar... but this isn't a Javascript forum.

              So I guess the real question is: What are you trying to do? Once we answer that, then we can know what language you need to write in and whether or not you are in the right forum.

                etully wrote:

                Since Javascript runs client side, it does have access to the address bar...

                NO, it does not, as I said in my previous post.

                Javascript can read the location of the current document, nothing more and nothing less. It can also read the location of other documents (e.g. in Frames, Iframes) IF they are in the same security domain.

                None of these things are necessarily the same as what's in the address bar- therefore Javascript CANNOT, in the general case, read what's in the address bar.

                Mark

                  Mark,

                  I did read your last post and you are right and I agree, Javascript does not have full 100% read/write/modfiy access to the URL field. But it can read the URL which is all he wants to do... so I should have said, "Javascript can know the URL of the page and redisplay the URL if you want".

                  I was addressing a more general issue for tvks which was that on one hand, he was saying, "Assuming that I am not at the server" and on the other hand, he was saying, "My intention is to have the value in a php variable like $address, which I can use to display".

                  So at this stage of the thread, understanding the difference between read and write access to the location bar in Javascript isn't really his biggest problem. We don't even know yet whether he wants to write client side or server side code (and I'm not sure he knows either) and my post was trying to help him get his bearings. I didn't feel the need to explain a finer point of Javascript since (A) you had already done it perfectly well and (😎 he may or may not need to be writing in Javascript.

                    8 days later

                    I agree with you eTully, JS doesnt have complete read/write access to the adressbar (if something does), and it does have read access, and some "write" capabilities (you can replace, or retrieve, operate, then resend, stuff like that).

                    But Mark, as far as I understand, I would disagree with you.

                    MarkR wrote:

                    "Javascript can read the location of the current document, nothing more and nothing less"..."therefore Javascript CANNOT, in the general case, read what's in the address bar."

                    As you said, in the address bar you can find:
                    $SERVER['HTTP_HOST'], $SERVER['SCRIPT_NAME'], $SERVER['PATH_INFO'], $SERVER['QUERY_STRING'] and a Hash (value after #, yeah "the fragment part").
                    That would be something like: http://www.domain.com/path/file.php?v1=u1&v2=u2#hash

                    (the http_host is more complex than just the "http://", but you do get some info about it on the adress bar's text)
                    Sure JS can read the location (including post vars and hash), is there anything more to read in the address bar?

                    "window.location.href" returns the first 4 (http, script, path, query), packed up in a whole string, as you should know. (You can even retrieve the post vars with javascript. (see )

                    On the other hand, "document.location.hash" is the hash object ("the fragment part"), which regulary indicates anchors within html documents, but can be used to send data via http (see, script in here).

                    If you understand the concept of dynamic content you should know the location includes where the document/program is, and both the actual state of vars, and also the hash which indicates a part of the html.

                    Anyway, there should be plenty of exceptions (from which I would like to see an example of) where the location is different from the content of the adress bar, which in general cases CAN be read by JavaScript, and if I take your word, even better than most server-side languages; because they don't recieve "the fragment part".

                    The way I think of it: they should work together to get things done.

                      TVKS you CAN retrieve "the fragment part" with JS (or the whole sentence) and then pass it to PHP and store it in your $address to work with it (echo, combine, operate, whatever).
                      The thing is changing the value of $address in PHP won't change the actual text you see in the address bar. Beacause JS has the object, PHP doesn't, we would only be passing the value as copy of the atual value -not reference or instance- you could write a script for dynamically refreshing the adress bar with the content of $address; so you can operate in PHP and then autosend the results to JS. But that would be latter I guess.

                      I would prefer sending a hidden value (if you can, don't know the particular case), and then retrieve it like any posted var, with something like:

                      if (isset($_POST['hiddenValue'])) {
                      	$hiddenValue = $_POST['hiddenValue'];
                      }
                      if ($hiddenValue = "") { 
                          /* be aware of the emptyness */
                      }
                      

                      You could assign the hidden value to a JavaScript's var, so you could:

                      <script type="text/javascript">
                      function throwHash() {
                          var hashValue;
                          if (document.location.hash.length == "0") { 
                              // no hash
                              hashValue = "";
                          } else {
                              // assign value
                      	hashValue = document.location.hash.substring(1,document.location.hash.length);
                          }
                      document.write("<input type='hidden' id='hiddenValue' value='"+hashValue+"'>"):
                      }
                      throwHash();
                      </script>
                      

                      What we try do here is get the value from the hash with JS and then put it to create a hidden form value, to send the hash via POST to PHP. this of course wont work in realtime (if the hash changes after first loading); for that we would have to actually write the hidden field and make its value the JS var, so the script can be executed several times and be always actual.
                      If the POST method of sending the value to PHP doesnt work for you, let us all know, we could be thinking of other way of doing it.

                      If what you need to do is just echo, or print the value in screen, maybe you don't even need to send to PHP, and can handle it from JS; since its a client-side feature, it would be more likelly to be easier.

                      Hope it helps.

                      (PS: Don't copy-paste that code, it probably won't work, it's just an idea of what it could do.)

                        Write a Reply...