1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Detecting HTML Page Title in PHP

Discussion in 'PHP' started by ProductivePC, May 29, 2004.

  1. #1
    I currently have a JavaScript that I am trying to convert over to php.
    In JavaScript to detect the page title would just be document.title.

    Is there a way to do this in php? :confused: If so,
    How do I detect a page title in php?
    I have to detect it no matter what the extension is ie .asp, .php, etc etc etc.

    Thanks for any insight and help :eek:

    Wayne
     
    ProductivePC, May 29, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    PHP is server-side, so you would need to download the page within the PHP script and evaluate it internally.

    What's the purpose exactly? That would be helpful if we knew what you were trying to do exactly...
     
    digitalpoint, May 29, 2004 IP
  3. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Currently, I am using a statcounter tracking code to track my websites...

    They reference a .js file in order to track. This .js file is located on their servers and they do not support https for SSL therefore I cannot track any of the websites within https parameters.

    I am trying to rewrite the .js file into a .php file, host the .php file on my secure server so the browser doesn't complain and then server-side send the information to statcounter.

    I have got most of it written out and changed however there are a few things in there that I don't know how to get such as the title of the html page and the location of the document....

    document.title
    document.location - This I believe I can use something to the effect of $_SERVER['HTTP_HOST']. I am just not sure exactly how to write it out to get that information.

    Thank you for the quick reply.
    If you want to see the .js file that I am talking about you can see it here.
    JS File

    Wayne
     
    ProductivePC, May 29, 2004 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    There is no global variable for the title, so you would either need to read the html file in and strip the title, or have the title somewhere in a user defined variable within your PHP script to reference.
     
    digitalpoint, May 29, 2004 IP
  5. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That is what I was afraid of.... well there are a few things that I could possiby do. I was just hoping to stay away from it. I could send the title through the URL to the php page and pick up with a variable that way. I just didn't want to have to do that. I am also making this for people that have no knowledge of programming whatsoever. I am not sure how to program ph to search the html page for whatever is between the title tags... is this easy to accomplish? Is this possible. to have it search for the <title>My Title</title> and take whatever is in between them no matter how many characters?
     
    ProductivePC, May 29, 2004 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    Well first you would need to get the whole page into a PHP variable (by downloading it or some other method).

    But it sounds like it may be more work than it's worth in your case. Why exactly do you need the title as a variable?
     
    digitalpoint, May 29, 2004 IP
  7. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have to copy the statcounter .js file and they currently have

    var sc_title = escape(document.title);
    var sc_url = escape(document.location);

    Since these are JavaSCript specific terms they are a little more difficult to copy over in PHP

    As I stated prior I know that with the document.location I can work something up with $_SERVER['HTTP_HOST']..... something that includes it whether it is https or https....
    I am just not exaclty sure how to write that yet.

    But with the Title.... I know of no way to get that information easily....

    How about this? Would this work
    I can include a php file on an html page by using the following code
    <script language="javascript" src="myphpfile.php"></script>

    If inside of that php file I have their javascript picking up the title
    var sc_title = escape(document.title);

    How would I transfer that to a php variable?
    I can pick it up in the php file with javascript.... I just don't know how to transfer it over to the php code.

    How would I get var sc_title to = $sc_title

    Does that sound like it would work?

    Wayne
     
    ProductivePC, May 29, 2004 IP
  8. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #8
    You can't transfer a JS variable to PHP because PHP is server side, and it's not processing by the time the page is spit out to the browser. You could have a JS file that calls a different PHP script for the logging I suppose though.
     
    digitalpoint, May 29, 2004 IP
  9. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    okay, how would that work?
    Wouldn't I still be in the same boat?
     
    ProductivePC, May 29, 2004 IP
  10. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #10
    Probably, yeah... your best bet would be to have the title in a PHP variable when you are generating the page with PHP.
     
    digitalpoint, May 29, 2004 IP
  11. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #11
    okay I understand it as this
    First we have the HTML pages gets loaded and then at the bottom of the HTML page we have the following
    <script language="javascript" src="get_title.js"></script>
    Inside that .js file we have a reference to a get-title.php file that includes the javascript for document.title and places it into var sc_title. This is not a problem.

    If we can do that why can we just not do this?
    <script language="javascript" src="get-title.php"></script>
    This way it would have to load the html page prior to loading the php file and then when it ran the php file it would then run the javscript. Now again, I am at the point of getting that JavaScript variable for the page title to appear in the variables for the next script whic hasn't been read yet.

    The next php file
    <script language="javascript" src="counter.php"></script>
    This is located after the first javascript and therefore will be read afterwards. The javascript will have full knowledge of variable title at that point in time. the only thing that I can think of is to send it throgh the URL but I don't really want to do that if I don't have to.

    Does that sound about right?
     
    ProductivePC, May 29, 2004 IP
  12. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The page is not being generated with PHP.... it is a static html page
    That would be too easy if we could just use php for everything :D
     
    ProductivePC, May 29, 2004 IP
  13. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If I remember correctly.... this goes off the php for a second but when you are generating a javascript you do not need the window.document.title correct because document.title will work just as good.

    I am asking because there are some references to if (window.variable) that I can very easily just change over to if ($variable)

    Isn't that the correct thinking?
     
    ProductivePC, May 29, 2004 IP
  14. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #14
    Well, if the src of your JavaScript file is a PHP file, then you are using PHP to generate JavaScript, but you cannot do any actual JS variable evaluation within that file.
     
    digitalpoint, May 29, 2004 IP
  15. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I was talking about outside of the <% %>

    We would have

    My javascript detect here
    <%
    My php stuff here
    %>

    The problem is getting My JavaScript over to
    <%
    My php
    %>

    I know this can be done if I send it through the url as such
    <script language="JavaScript">
    var image = "http://www.mysite.com/pixel.gif";

    var sc_title = escape(document.title);

    var link = "<img src='https://www.mywebsite.com/myphpfile.php?title=" + title + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";

    document.write(link); //This will write out to the page
    </script>


    however once that is there if I use the

    $sc_title = $_GET['sc_title'];
    then I should be able to transpose the title into PHP with no problem

    Again, I did not want to go this route because I ammaking this for people that do not know anything about coding or programming. I want this to be as close to plug-n-play as possible. This would require them putting an a pixel image on their webpage.

    I can hear the tech support calls now... :p

    That script doesn't look totally correct to me but I believe that you know what I mean

    Any suggestions and does that script look right to you?
     
    ProductivePC, May 29, 2004 IP
  16. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #16
    That general idea is your best bet... (passing the title as a variable to a PHP script)
     
    digitalpoint, May 29, 2004 IP
  17. ProductivePC

    ProductivePC Peon

    Messages:
    362
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #17
    okay that is what i will do.

    I just have one last question and not really for me but more for the people reading this post. I cannot remember. However testing the script will tell me which ones works.

    When I am writing
    <script language="JavaScript">
    var image = "../pixel.gif";
    var sc_title = escape(document.title);
    var title-src = "<img src='../myphpfile.php?title=" + sc_title + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";
    document.write(title-src); //This will write out to the page
    </script>

    Do I write it as:

    <script language="JavaScript">
    var image = "../pixel.gif";
    var sc_title = escape(document.title);
    var title-src = "<img src='../myphpfile.php?title=" + sc_title + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";
    document.write(title-src); //This will write out to the page
    </script>
    which references the document.title

    OR

    <script language="JavaScript">
    var image = "../pixel.gif";
    var sc_title = escape(document.title);
    var title-src = "<img src='../myphpfile.php?sc_title=" + sc_title + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";
    document.write(title-src); //This will write out to the page
    </script>
    which references the variable?

    I haven't tested the scripts yet and don't remember off hand.

    Thanks for all of your quick replies Shawn :cool:
     
    ProductivePC, May 29, 2004 IP
  18. williamscraig

    williamscraig Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    /*
    This is in the php manual at:
    http://us2.php.net/manual/en/features.remote-files.php
    A similar technique will work for locals, and you can tweak it for finding the title when it's not alone on the line.
    */

    <?php
    $file = fopen ("http://www.example.com/", "r");
    if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
    }
    while (!feof ($file)) {
    $line = fgets ($file, 1024);
    /* This only works if the title and its tags are on one line */
    if (eregi ("<title>(.*)</title>", $line, $out)) {
    $title = $out[1];
    break;
    }
    }
    fclose($file);
    ?>
     
    williamscraig, Jul 30, 2004 IP
  19. marcosxsilva

    marcosxsilva Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Another way to get HTML title:

    function get_remotetitle($urlpage)
    {
    		$dom = new DOMDocument();
    
    		if($dom->loadHTMLFile($urlpage)) {
    
    			$list = $dom->getElementsByTagName("title");
    			if ($list->length > 0) {
    				return $list->item(0)->textContent;
    			}
    		}
    }
    PHP:
    In this case, you don´t have to take care if the "title itself" and its tags (<title> and </title>) are in the same line.

    I´m not sure if DOM functions are available in PHP4... It worked in PHP5... Remember to deactivate DOMXLM module in php.ini (DOM and XML communities don´t talk the same language) ;-)

    Rgds,
    Marcos
     
    marcosxsilva, Oct 2, 2007 IP
  20. abdulrahman bahajaj

    abdulrahman bahajaj Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    it is easy guys
    just write this
    <?
    $html=file_get_contents('http://www.your_web_page.com');
    //here we have get the contents of this webpage as a var i called it $html
    //and now u have to split this var tell u get the title it easy but it need a work....
    ?>
     
    abdulrahman bahajaj, Jan 6, 2010 IP