...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 18, 2011

How to get a website's favicon with PHP?

Question by xy_

I want to get requested website’s favicon with PHP. I have been recommended using Google’s favicon service but it is not functional. I want to do something on my own but don’t know regex usage.

I found a class on Google that works on most cases but it has unacceptable error rate. You can have a look here: http://www.controlstyle.com/articles/programming/text/php-favicon/

Can somebody please help me about getting favicon using regex, please?

Answer by vooD

Quick and dirty:

<?php 
$url = 'http://example.com/';
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(file_get_contents($url));
$xml = simplexml_import_dom($doc);
$arr = $xml->xpath('//link[@rel="shortcut icon"]');
echo $arr[0]['href'];

Answer by Starx

Use the S2 service provided by google. It is as simple as this

http://www.google.com/s2/favicons?domain=www.yourdomain.com

Scraping this would be much easier, that trying to do it yourself.

Read more

jquery toggle button value

Question by Rajeev

In the below code when the play button is clicked Its value should be changed to pause and when pause is clicked the other function should be called .How to do this using jquery toggle

  <input type="button" onclick ="play" value="play"/>
   <script>
     function play()
     {
               play_int();
           //   Button value to be changed to pause
     }   

  And when pause play_pause();

Answer by Town

Give your button an ID:

<input type="button" id="play" value="play"/>

Then you can do something like this:

$('#play').click(function() {
   if ($(this).val() == "play") {
      $(this).val("pause");
      play_int();
   }
   else {
      $(this).val("play");
     play_pause();
   }
});

Or a slightly tidier version like this:

$(function(){
    $('#play').click(function() {
       // if the play button value is 'play', call the play function
       // otherwise call the pause function
       $(this).val() == "play" ? play_int() : play_pause();
    });
});

function play_int() {
    $('#play').val("pause");
    // do play
}

function play_pause() {
    $('#play').val("play");
    // do pause
}

Working demo

Answer by Starx

A simplest idea can be this

function play(action) {
 { 
   if(action=='play') { 
      play_int(); 
      $("#btn_play").val('pause');
   }
   else { 
      pause_int(); 
      $("#btn_play").val('play');
   }
}

$("#btn_play").click(function() {
   val = $(this).val();
   play(val);
}
Read more

Restricting access to a web directory using .htaccess

Question by Sanks Raut

I want to restrict access to certain web directories in my website using .htaccess file. I know one way using

order allow,deny
deny from all

But it restricts only the first directory. It doesn’t work for the remaining ones. Please help

Answer by jimy

Options -Indexes add this line at the top of the .htaccess file

Answer by Starx

You are wrong, using deny from all also applies to subdirectories.

i.e. unless there is another .htaccess inside the other sub directories that allows the access

Read more

Why does this attribute not change?

Question by daniel.tosaba

In the code below the last line is not setting the src attribute. Why?

$("div.galerina").each(function(){
    var gal=$(this);
    var bullet=gal.children("img.galerina1");
    var width=12*(gal.children("img").size())+'px';
    gal.children("p").css({width:width})

    gal.children("img").each(function(){
    var img=$(this);
    gal.children("p").append("<img class='bullet' src='images/bullet1.png'></img>");
    $("img.bullet").last().click(function(){
        bullet.animate({opacity:0},300,function(){
            bullet.css({display:'none'});
            bullet=img;
            img.css({display:'block'});
            img.animate({opacity:1},300);
        });

    })
})
gal.children("img.bullet").first().attr("src","images/bullet2.png")
})

LIVE VERSION HERE!!!

Answer by Frédéric Hamidi

gal in not in scope anymore when you try to use it to change the bullet image.

You’ll need to either move your last line inside the function passed to $("div.galerina").each(), or reinitialize gal before you use it:

$("div.galerina").each(function() {
    var gal = $(this);
    gal.children("img.bullet").first().attr("src", "images/bullet2.png");
});

Answer by Starx

Just try it using gal.children because gal is already initiated at $(this). No need to do $(gal)

Same goes with $(img)

Read more
April 17, 2011

Ajax comments insert LI or replace UL?

Question by Aen Tan

I’m ajaxifying my comments feature and I’m choosing between 2 strategies. My comments list is an unordered list.

  1. Replace UL in DOM with UL from response.
  2. Insert last LI from response into UL in DOM.

The first option is simpler but I feel like it’s not the best or right way.
If I want to use the second option how do I know which comments in the response are newer than the ones in the DOM if more than one comment was posted at the same time?

Thoughts?

Answer by JohnP

You can keep the timestamp as a variable for JS to refer.

When you load the page, your server side language will assign the timestamp of the last comment to be loaded to a javascript variable. From there on, everytime you load more comments, send the last timestamp you requested. Your server side language will check for comments that are newer than that timestamp. Make sure to update the timestamp as you send out more requests. Either by sending it as part of the response or keeping track of when you sent the request.

Answer by Starx

You should append the comment’s UL with latest LIs. Why? you do not have to grab all the full UL list and replace the old one. Reduce the changes that has to be made. Just grab the latest LI and append it at the last. LESS WORK MORE EFFICIENT

So far of your question, about how to know which one is the latest comment. Store timestamp as the comments are stored in the DB. While also fetching the comments for the first time, try to assign the variable holding the timestamp of the last item, then after search the database for everything that is greating the timestamp you stored, get the list and append them, then update the variable, with the latest time stamp again.

Read more
April 13, 2011

php function returning a string with a new line?

Question by Jorge

In my first 3 days in coding my homework. I successfully did it. But when I got to school, my code have a problem. I have an ajax form and some php functions.

Here is an example.

$i = 1
if($i == 1) {
   echo "yes";
}
else {
   echo "no";
}

The answer in my sample is true. But in my case it’s always false. And I notice the ajax requests that the returned message was

    // new line
    // new line
yes
    // new line

That’s why it’s being false. What is the cause of this? I didn’t put any new line in my code.

Answer by Starx

If you want the easy way out of it, try this

ob_clean();
echo "yes";
exit;
Read more
April 11, 2011

login through script to any website

Question by RAJESH BANSAL

I have username and passwords in database table for many websites. Whenever I need to go through the website I have to copy and paste usernames and passwords to login. Which is very time consuming.

So I need a php script by which I only need to click the link of website which I need to login and by using username and password from database, it automatically submit login form and redirect me to after login web page.

I hope you understand my need.

Answer by Starx

I am not sure of what is being asked here. FTP Password and CPanel Password are not possible to be logged in through your script. But for admin panel, it is possible (not that I am encouraging it), using a hidden form.

<form name="hiddenform1" action="http://thetargetwebsite.com/login.php" method="post">
    <input name="user" id="user" type="hidden" value="theusername" />
    <input name="password" id="password" type="hidden" value="thesecretpassword" />
    <a href="#" onClick="document.forms["hiddenform1"].submit();">The almightly link</a>
    <!-- Or modify the submit button to make it look like a link -->
</form>

After, this the targetted website, will use the username and password sent by your form to login.

Read more
April 10, 2011

JQuery .post() not working at latest version? SOLVED

Question by Eric T

Something happen with later version of Jquery where it work fine with 1.4.1,1.4.2,1.4.4…
Below is the code that I tried.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" 
    type="text/javascript" charset="utf-8"></script>

Test post.php is a empty file with just opening of <?php ?>

$.post("testpost.php",function(data){
                        alert("Hello World");
                      });

Arghhhh I found the problem that was the button type where it couldn’t be submit so instead of submit I changed to button and finally it works. Thanks everyone for your quick response.

Answer by EmCo

Maybe the alert("Hello world") is not displayed because you are not receiving any response from the server. The function(data){...} is only called on success.

Answer by Starx

First check if testpost.php exists and the path is correct.

On what event is the post request sent? Try wrapping it inside $(document).ready();

$(document).ready(function() {
    $.post("testpost.php",function(data){
         alert("Hello World");
    });
});

Even if this also dont give you a response. Update your question with the code inside testpost.php.

Read more
April 5, 2011

Why does the size of a div element change when changing position from fixed to absolute?

Question by ThiefMaster

I have a div containing some text. This div used to be position:fixed, but for some reasons I need it to be position:absolute.

However, when changing it from fixed to static its size changed (the “auto-sizing” during fixed display was nice and should be preserved).

Here’s a minimal example: http://jsfiddle.net/ThiefMaster/yBRa9/3/

I’m looking for a way to keep position:absolute without the element shrinking to the lowest possible size.

Using JavaScript it’s easy to achieve but if it could be done without additional JS it would be nice.

Answer by Starx

Here Check it. I used a span with inline-block, seems to do what you want (Of course, If I had understood your question properly).

Read more
...

Please fill the form - I will response as fast as I can!