Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End trouble with $_SERVER["REQUEST_URI"]

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #173178
    bearhead
    Participant

    Hello, I’m using the flowing php to return the url of the current page:

    <?php
    function curPageURL() {
    $isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
    $port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
    $port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
    $url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
    return $url;
    }
    ?>

    However, instead of returning the full url of the current page, it just returns the root website folder.

    Here it is live:
    http://www.aeieng.com/home_test.php

    if you scroll down to the footer, and use the linkedin sharing button, you will notice that it creates a link to: http://www.aeieng.com/
    instead of:
    http://www.aeieng.com/home_test.php

    I really don’t know much about php, so I’m pretty clueless as top what the issue here is… any help would be awesome! :)

    #173191
    __
    Participant

    Your function works as expected (i.e., includes the file path) when I test it. Can you show the code where you actually use this? are there any errors in your error log?

    A few suggestions unrelated to your specific problem:

    • don’t worry about http: vs https:. As long as this is only for html (not plain text), just start the url with // and the browser will default to whichever protocol it is already using.
    • unless you expect to use non-default ports (unlikely), don’t worry about that either (just leave $port out).
    • use basename( $_SERVER['REQUEST_URI'] ) to make sure you’re only printing the file name, and not any path info or the query string. This will prevent mistakes, as well as close a potential XSS vulnerability.
    #173200
    bearhead
    Participant

    If you hover over the linkedin button (in the footer) and look at the url that pops up in the bottom of the browser window, you’ll notice that it reads:

    http://www.linkedin.com/shareArticle?mini=true&url=http://www.aeieng.com&title=Click%20here&summary=Add%20summary&source=aeieng.com

    where as it should read:

    http://www.linkedin.com/shareArticle?mini=true&url=http://www.aeieng.com/home_test.php&title=Click%20here&summary=Add%20summary&source=aeieng.com

    which leads me to believe that some how the code isn’t retrieving the full url, or its only echoing out the root folder for some reason?

    #173204
    __
    Participant

    I understand your question, and what you expect vs. what you see on the webpage. I am asking to see the code you actually use to help find where the problem might be: as I explained, the function you posted works as expected when I test it.

    Please also check your error logs and let us know what you find.

    #173493
    bearhead
    Participant

    Here is the code for the body of my document:

    The php script is right below the facebook javascript, and I’m echoing out the url in the ordered list lower down the code

    <body >
    
    <script>
    window.fbAsyncInit = function(){
    FB.init({
        appId: 'xxxxx', status: true, cookie: true, xfbml: true }); 
    };
    (function(d, debug){var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];if   (d.getElementById(id)) {return;}js = d.createElement('script'); js.id = id; js.async = true;js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";ref.parentNode.insertBefore(js, ref);}(document, /*debug*/ false));
    function postToFeed(title, desc, url, image){
    var obj = {method: 'feed',link: url, picture: 'http://www.url.com/images/'+image,name: title,description: desc};
    function callback(response){}
    FB.ui(obj, callback);
    }
    </script>
    <?php
    function curPageURL() {
    
    $url = ('http://').$_SERVER["SERVER_NAME"].$port.basename( $_SERVER['REQUEST_URI'] );
    return $url;
    }
    ?>
    
    <div id="footer_body">
    <div class="container" >
      <div class="footerbg">
      
      <div id="footerleft" class="eight columns"><a href="#">Back to top &raquo;</a><br /><br /><a href="#">Contact us</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">About us</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">News</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#">Conferences</a><br /><br /><span class="footercopy">Affiliated Engineers, Inc. &copy; 2014</span>
      </div> 
        
     <div id="footerright" class="eight columns">
     <ul class="social">
     <li><a href="javascript:if(window.print)window.print()"><span class="aeicon icon-printer"></span></a></li>
     <li><a href="mailto:[email protected]?Subject=Hello"><span class="aeicon icon-mail" ></span></a></li>
     <li><a href="https://twitter.com/share"  data-count="none"><span class="aeicon icon-twitter" ></span></a></li>
     <li><a href="http://www.linkedin.com/shareArticle?mini=true&url=<?php
      echo curPageURL();
    ?>&title=Click%20here&summary=Add%20summary&source=aeieng.com"><span class="aeicon icon-linkedin" ></span></a></li>
      <li><a href="" data-image="" data-title="" data-desc=""  class="btnShare"><span class="aeicon icon-facebook" ></span></a></li>
    
        
        </ul>
      </div>
      
    </div>
    </div><!--end container-->
    </div>
    <script>
    $('.btnShare').click(function(){
    elem = $(this);
    postToFeed(elem.data('title'), elem.data('desc'), elem.prop('href'), elem.data('image'));
    
    return false;
    });
    </script>
    
    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>  
    
    </body>

    I’m not very proficient with php, so I have no idea if I’ve implimented it correctly or not…

    I don’t follow you on the error logs… I don’t know how to view php errors, or are you looking for errors found with the w3c validator?

    #173499
    __
    Participant
    function curPageURL() {
    
    $url = ('http://').$_SERVER["SERVER_NAME"].$port.basename( $_SERVER['REQUEST_URI'] );
    return $url;
    }
    

    I think you mangled the function a bit trying to change it. I wouldn’t worry about it for now —just use the original function you had.

    Also, I was wrong to suggest using the basename function. I got mixed up in what I was thinking about. You actually want to use parse_url:

    $path = parse_url( $_SERVER["REQUEST_URI"],PHP_URL_PATH );
    

    However, this is not why it isn’t working. Your original function works when I test it, and I don’t see anything in your code that would affect its output.

    I don’t follow you on the error logs… I don’t know how to view php errors, or are you looking for errors found with the w3c validator?

    No, I am talking about PHP errors. Contact your hosting company and ask where your PHP error log is. (Often, it will be in the directory above your web root, named something like php_errors.log or errors.txt).

    Alternatively, at the very top of your script, you could put these two lines:

    error_reporting( -1 );
    ini_set( 'display_errors','on' );
    

    …which will cause PHP’s error messages to be printed to the browser.
    Be sure to remove them once you are done debugging.

    #173589
    bearhead
    Participant

    I used
    $_SERVER["PHP_SELF"]
    instead of
    $_SERVER["REQUEST_URI"]

    and its returning the proper url now!

    I really have no idea why that’s returning the url correctly… maybe it has something to do with my server? I dunno.. at least its functioning the way I need it to :)

    #173611
    __
    Participant

    I used $_SERVER["PHP_SELF"] instead of $_SERVER["REQUEST_URI"]

    You should still parse the url, to prevent xss attacks.

    $path = parse_url( $_SERVER["PHP_SELF"],PHP_URL_PATH );
    

    Glad it’s working.

    maybe it has something to do with my server?

    It could. Are you using Apache? do you use mod_rewrite? are you sure your server populates REQUEST_URI (though I don’t know why it wouldn’t)?

    You could try var_dump( $_SERVER ) to see what’s available.

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.