Jump to content

gerkintrigg

Members
  • Posts

    830
  • Joined

  • Last visited

Everything posted by gerkintrigg

  1. // start ajaxrequest.js // Following is a javascript function that makes a httprequest - AJAX. This is the // AJAX bit and all that is needed in that manner. // Only in this one we won't be using XML in our response, we will accept and handle // pure text and html and display this response directly to the user within the // desired <div id> tags. It can even be used to include pure html files as a substitute // solution to the "old" frames method where as no php or other scripting language is // nessesary on the server. // but use it with care - it is not a search engine supported method and indexing will // fail. Workaround for this is not included here function MyAjaxRequest(target_div,file,check_div){ var MyHttpRequest = false; var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />'; var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead'; if(check_div){ var check_value = document.getElementById(check_div).value; } else{ var check_value = ''; } if(window.XMLHttpRequest){ // client use Firefox, Opera etc - Non Microsoft product try{ MyHttpRequest = new XMLHttpRequest(); } catch(e){ MyHttpRequest = false; } } else if(window.ActiveXObject){ // client use Internet Explorer try{ MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){ try{ MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ MyHttpRequest = false; } } } else{ MyHttpRequest = false; } if(MyHttpRequest){ // browser supports httprequest var random = Math.random() * Date.parse(new Date()); // make a random // string to prevent caching var file_array = file.split('.'); // prepare to check if we have a query string or // a html/htm file if(file_array[1] == 'php'){ // no query string, just calling a php file var query_string = '?rand=' + random; } else if(file_array[1] == 'htm' || file_array[1] == 'html'){ // calling a htm or //html file var query_string = ''; } else{ // we have presumable a php file with a query string attached var query_string = check_value + '&rand=' + random; } MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET // handle the httprequest MyHttpRequest.onreadystatechange = function (){ if(MyHttpRequest.readyState == 4){ // done and responded document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result } else{ document.getElementById(target_div).innerHTML = MyHttpLoading; // still working } } MyHttpRequest.send(null); } else{ document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest } } // end of "AJAX" function // Here follows a function to urlencode the string we run through // our httprequest, it has nothing to do with AJAX itself // If you look carefully in the above httprequest you se that we use this // url_encode function around the file and query_string // This is very handy since we are using GET in our httprequest and for instance // any occurrance of the char # (from textboxes etc) will brake the string we are //sending to our file - we don't want that to brake! // It will also convert spaces to + function url_encode(string){ var string; var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?="; var hex = "0123456789ABCDEF"; var encoded_string = ""; for(var i = 0; i < string.length; i++){ var character = string.charAt(i); if(character == " "){ encoded_string += "+"; } else if(safechars.indexOf(character) != -1){ encoded_string += character; } else{ var hexchar = character.charCodeAt(0); if(hexchar > 255){ encoded_string += "+"; } else{ encoded_string += "%"; encoded_string += hex.charAt((hexchar >> 4) & 0xF); encoded_string += hex.charAt(hexchar & 0xF); } } } return encoded_string; } // end .js file
  2. I thought I'd post this in case someone finds it useful. It is a basic script I wrote to check whether links are valid within a grabbed piece of HTML code. I'm fairly convinced that this is not the best way to do it, but it works and is fairly quick, so if you want to use it, go ahead. <span class="title">Website links Check:</span><br /> <?php // a simple string $string = $_SESSION['page']; // put the matches in a variable called matches $url=preg_match_all('~<a href="(.*?)"(.*?)>~', $string, $matches); // loop through the array of matches and print them foreach($matches as $key=>$value){ if($key==1){ foreach($value as $second_key=>$second_value){ if(!$fp = @fopen($second_value, 'r')){ echo '<a href="'.$second_value.'" target="_blank">'.$second_value.'</a> - INVALID<br>'; } else{ echo '<a href="'.$second_value.'" target="_blank">'.$second_value.'</a> - OK<br>'; } } } } ?> A back link would be nice too.
  3. Firefox does nothing at all when I click on the submit button. It doesn't even submit. here is the ajax script. Let me know if you need anything else // start ajaxrequest.js // Following is a javascript function that makes a httprequest - AJAX. This is the AJAX bit and all that is needed in that manner. // Only in this one we won't be using XML in our response, we will accept and handle // pure text and html and display this response directly to the user within the // desired <div id> tags. It can even be used to include pure html files as a substitute // solution to the "old" frames method where as no php or other scripting language is nessesary on the server. // but use it with care - it is not a search engine supported method and indexing will fail. Workaround for this is not included here function MyAjaxRequest(target_div,file,check_div) { var MyHttpRequest = false; var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />'; var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead'; if(check_div) { var check_value = document.getElementById(check_div).value; } else { var check_value = ''; } if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product { try { MyHttpRequest = new XMLHttpRequest(); } catch(e) { MyHttpRequest = false; } } else if(window.ActiveXObject) // client use Internet Explorer { try { MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { MyHttpRequest = false; } } } else { MyHttpRequest = false; } if(MyHttpRequest) // browser supports httprequest { var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file if(file_array[1] == 'php') // no query string, just calling a php file { var query_string = '?rand=' + random; } else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file { var query_string = ''; } else // we have presumable a php file with a query string attached { var query_string = check_value + '&rand=' + random; } MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET // handle the httprequest MyHttpRequest.onreadystatechange = function () { if(MyHttpRequest.readyState == 4) // done and responded { document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result } else { document.getElementById(target_div).innerHTML = MyHttpLoading; // still working } } MyHttpRequest.send(null); } else { document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest } } // end of "AJAX" function // Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself // If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string // This is very handy since we are using GET in our httprequest and for instance // any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake! // It will also convert spaces to + function url_encode(string) { var string; var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?="; var hex = "0123456789ABCDEF"; var encoded_string = ""; for(var i = 0; i < string.length; i++) { var character = string.charAt(i); if(character == " ") { encoded_string += "+"; } else if(safechars.indexOf(character) != -1) { encoded_string += character; } else { var hexchar = character.charCodeAt(0); if(hexchar > 255) { encoded_string += "+"; } else { encoded_string += "%"; encoded_string += hex.charAt((hexchar >> 4) & 0xF); encoded_string += hex.charAt(hexchar & 0xF); } } } return encoded_string; } // end .js file
  4. in theory... I'm unsure of the syntax as, rather than just index.(whatever) , it would also need to cope with whatever.whatever
  5. Now, it is pretty-much working as I want it to, but I do have an issue with file name extensions: If the URL is http://www.myointernational.com or http://www.myointernational.com/ (with the forward slash), both work fine. If, however it is: http://www.myointernational.com/index.php then the grabbing of images fails. Any suggestions? (And I re-ask thebadbad about the post above too).
  6. like the ajax code? function MyAjaxRequest(target_div,file,check_div) { var MyHttpRequest = false; var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />'; var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead'; if(check_div) { var check_value = document.getElementById(check_div).value; } else { var check_value = ''; }
  7. weird... i am unsure whether the test server will help as it's acting rather strange... <div id="credits_used"><form name="url_submission" action="javascript: MyAjaxRequest('credits_used','<?php echo $root;?>includes/credit_calculator.php?url='+url_submission.url.value);" method="post"> <div align="right"> Enter a Web Address: <input name="url" type="text" class="input" /> <input name="submit_url" type="submit" class="button" id="submit_url" value="Submit"/> </div> </form></div> i was using it with a javascript realtext box, but removed it for the test server: http://www.myointernational.com/shine/test.php
  8. Oh, yes, there were a few issues with Firefox's cupport for the CSS code: cursor: hand; but I removed all references by commenting them out /*cursor: hand;*/ Still the form is not submitting. I'll put it up on a test server so you can see what I mean.
  9. @thebadbad, mainly because I didn't understand it and couldn't get it to work... if I used your function to parse the url of the root page (not necessarily a directory, but also a file) as $absolute and the link as $relative; would that work? I can try it, but need to test the current system first... I have a few issues with Firefox and ajax which need sorting before I implement any changes to the current script.
  10. So you want: SELECT *, COUNT(*) as num_replies FROM forum_replies LEFT JOIN forum_topics ON ( forum_replies.tid = forum_topics.id ) WHERE `cid`='".$row['id']."' ORDER BY lastposttime DESC"; etc?
  11. have you looked into explode() or preg_replace()? Though you might be better off with some kind of preg_match function and then loop through the matches. Try a for each loop.
  12. hello, I'm using this code to link a form to an ajax include: <form name="url_submission" action="javascript: MyAjaxRequest('credits_used','../includes/credit_calculator.php?url='+url_submission.url.value);" method="post"> It works fine in chrome, but not in firefox. I'm fairly sure it's this line that's causing the issue. Javascript seems to be active. Any ideas?
  13. Okay, so I still don't know how to get the variable to work, but I removed the need for it by putting the content of the get_src() function inside the callback like this: #-------------------------- get the right URLs ------------------------------ function real_links($matches){ global $my_url; #------------ function -------------- $tmp_url = rtrim($my_url, '/'); $path = ltrim($matches[1], '\\'); if(($num_of_them = substr_count($path, '../')) > 0) { $tmp_url = preg_replace("#(/[a-z0-9-]+){{$num_of_them}}$#iD", '', $tmp_url); $path = $tmp_url . '/'. str_replace('../', '', $path); } else{ $path=str_replace($path,($tmp_url.'/'.$path),$path); } #--------- end function --------- return 'src="'.$path.'"'; } $page=preg_replace_callback('~src="(.*?)"~','real_links',$page); echo $page;
  14. sorry, I edited that last post because I thought it was confusing too... it outputs this code: Display This: <img src="http://www.google.com/one_level_up/2_levels_up/3levels_up/intl/en/images/logo.gif"> Don't Display This: <img src="http://www.google.com/one_level_up/2_levels_up/3levels_up/intl/en/images/logo.gif"> The $my_url variable doesn't perform the operations to it that the get_src() function is supposed to be performing (and DOES perform when it's hard-coded)... If it's not a global, it returns: Display This: <img src="/intl/en/images/logo.gif"> Don't Display This: <img src="/intl/en/images/logo.gif"> which is not right either... I think the global is handy. Can i define the get_src() function as global too?
  15. Thanks Thorpe. That looks like a handy tip. I tried that out and read through the literature for the global command but the following code doesn't remove the folder options like the get_src() function should and doesn't act quite like the hard-coded version: <?php $url='http://www.google.com/one_level_up/2_levels_up/3levels_up/'; $my_url=$url; $path = '../../intl/en/images/logo.gif'; #this should display $path2 = '../../../intl/en/images/logo.gif'; # this should not $page='<img src="'.$path.'"> Don\'t Display This: <img src="'.$path2.'">'; #------------------------- Function Below -------------------------------- function get_src($tmp_url,$path){ $tmp_url = rtrim($tmp_url, '/'); $path = ltrim($path, '\\'); if(($num_of_them = substr_count($path, '../')) > 0) { $tmp_url = preg_replace("#(/[a-z0-9-]+){{$num_of_them}}$#iD", '', $tmp_url); $path = $tmp_url . '/'. str_replace('../', '', $path); } else{ $path=str_replace($path,($tmp_url.'/'.$path),$path); } return $path; } ?>Display This: <?php #-------------------------- get the right URLs ------------------------------ function real_links($matches){ global $my_url; return 'src="'.get_src($my_url,$matches[1]).'"'; } $page=preg_replace_callback('~src="(.*?)"~','real_links',$page); echo $page; ?>
  16. In my first example I showed how I could get the correct URLs from any web page. By replacing the $url variable with $_POST['url'], this can easily be changed to react to user input, but it's the fact that the (currently static) variable in the callback function doesn't like to be made a variable that's causing all my problems. while the web page url can be defined from a form post, the callback needs to be defined in the code itself. I'm not sure whether the syntax is wrong or I am just doing something that's not strictly allowed. To clarify, this line: return 'src="'.get_src('http://www.google.com/one/two/',$matches[1]).'"'; will not work if it read like this: return 'src="'.get_src($tmp_url,$matches[1]).'"';
  17. Thanks, but the $url variable should be able to be any url, not only a local one...
  18. I want to get absolute URLs for my screen-scraper app that will get HTML code and render it in the browser with a few changes to spelling etc. To make sure it grabs CSS and images properly I have played about with different ways of getting absolute URLs I ended up with the following code: <?php $url='http://www.google.com/one_level_up/2_levels_up/3levels_up/'; $path = '../../intl/en/images/logo.gif'; #this should display $path2 = '../../../intl/en/images/logo.gif'; # this should not $page='<img src="'.$path.'"> Don\'t Display This: <img src="'.$path2.'">'; #------------------------- Function Below -------------------------------- function get_src($tmp_url,$path){ $tmp_url = rtrim($tmp_url, '/'); $path = ltrim($path, '\\'); if(($num_of_them = substr_count($path, '../')) > 0) { $tmp_url = preg_replace("#(/[a-z0-9-]+){{$num_of_them}}$#iD", '', $tmp_url); $path = $tmp_url . '/'. str_replace('../', '', $path); } else{ $path=str_replace($path,($tmp_url.'/'.$path),$path); } return $path; } ?>Display This: <?php #-------------------------- get the right URLs ------------------------------ function real_links($matches){ return 'src="'.get_src('http://www.google.com/one/two/',$matches[1]).'"'; # you'll note that the Google url needs to be defined, rather than a variable... why is that? } $page=preg_replace_callback('~src="(.*?)"~','real_links',$page); echo $page; ?> You'll note that the Google url needs to be defined, rather than a variable... why is that? How can I replace it with the $url variable at the top of the code, without causing an error?
  19. I wonder if someone could provide the right syntax for this:? # now replace the "src=" urls with real ones: $pattern='~src="(.?*)"~'; $new_url='~src="get_src($1,$path)"~'; $page = preg_replace($pattern, $new_url, $page);
  20. I think that this may work within a preg_replace function: <?php $url='http://www.myointernational.com/1/2/3/4/5/6/7'; $path = '../../../../../../../furniture/health_check_box.jpg'; ?> <?php function get_src($url,$path){ $url = rtrim($url, '/'); $path = ltrim($path, '\\'); if(($num_of_them = substr_count($path, '../')) > 0) { $url = preg_replace("#(/[a-z0-9-]+){{$num_of_them}}$#iD", '', $url); $path = $url . '/'. str_replace('../', '', $path); } else{ $path=str_replace($path,($url.'/'.$path),$path); } return $path; } echo '<img src="'.get_src($url,$path).'">'; ?>
  21. oh, now that is very handy! Thanks thebadbad
  22. thanks cags, I sorted the issue if there is no '../' by doing this: <?php $url = "myointernational.com/1/2/3/4/"; $path = '../../../../furniture/health_check_box.jpg'; $url = rtrim($url, '/'); $path = ltrim($path, '\\'); if(($num_of_them = substr_count($path, '../')) > 0) { $url = preg_replace("#(/[a-z0-9-]+){{$num_of_them}}$#iD", '', $url); $path = $url . '/'. str_replace('../', '', $path); } else{ $path=str_replace($path,($url.'/'.$path),$path); } echo $path; ?> I'll look into preg_match_all() though. Thanks.
  23. Thanks cags, that's great! The only issue I have with this is that I want to replace the path for every src="WHATEVER" Is there a way i can embed this within another preg_replace() function to loop through an entire document? Also it doesn't work if the path contains no ../ but that's no major big deal because I can just replace ../ with the URL using str_replace(); Thanks again.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.