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? 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 Wayne
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...
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
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.
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?
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?
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
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.
Probably, yeah... your best bet would be to have the title in a PHP variable when you are generating the page with PHP.
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?
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
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?
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.
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='©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... 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?
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='©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='©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='©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
/* 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); ?>
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
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.... ?>