February 29, 2012

Can't include file on remote server

Question by user918712

My problem is that I can’t include a file on a remote server.

<?php
  echo "Includingn";
  require_once("http://xx.xxx.xxx.xx:8080/path/to/myfile.inc");
  echo "Done..n";
?>

The script fails at the require_once function.
I’m running the script with: php -d allow_url_include=On script.php
but to make sure I have set allow_url_include and allow_url_fopen to On in php.ini

If I copy http://xx.xxx.xxx.xx:8080/path/to/myfile.inc to the browser I’m served the file.
I have also tried to include other remote files (on standard port 80), but still no luck

Where I get really confused is that everything works from my local computers at my office (mac, ubuntu), but not from our servers. I have tested it on 2 different servers, a virtual and a dedicated.
I can get the file with fopen().

Answer by Starx

This can be done by setting allow_url_include to on on php.ini.

But, as mentioned in comments, this opens a

huge

security hole on your application.

Get HTML source code as a string

Question by user1196522

I want the source code of an HTML page (1.html) to be used in another page (2.html). Further, I want to perform operations on it in 2.html.

Is there a way to do this?

EDIT: 1.html is a seperate public webpage and I do not have access to make changes to its source code. I have to do whatever I need only by using 2.html.

Answer by Starx

Its very simple

On 2.html use this jQuery snippet

$.get("1.html", function(response) { 
    alert(response) 
    //do you operations
});

htaccess for own php mvc framework

Question by paganotti

Good Morning,
I’m building my own php framework. I would want that all request pass to index.php file except request to js css jpeg, jpg, png file. So I want that a module independently to others has own assets folder with css images and js file.

The Structure is something similar:

application
   module1
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php

   module2
     models
     controllers
     templates
         assets
             css
             images
               test.jpg
               hello.png
             js

         view1.html.php
         view2.html.php
  core
    here is core file framework

  index.php

How Can I do it?

Update

My modules are under application folder. assets folder must be public.
I want a simple small url like www.example.com/module1/images/test.jpg

instead long

www.example.com/application/module1/templates/assets/images/test.jpg

Answer by Starx

Actually, the default .htaccess of zf works well for this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

How to use images for check boxes using CSS or may be Javascript?

Question by Imran

It is obviously very simple question but I have never done such thing before. My designer want some thing like this (it is on left side. Checked ones are grey and other white) for check boxes. Any help regarding this will be appreciated.

Answer by KooiInc

One idea is to use a span with background-image (empty box), hide a related checkbox, and assign a clickhandler to the the span (onclick: change checked attribute of the related hidden checkbox, change background image of the span accordingly).

Here is an example of that

Prevent hotlinking to files using hashes?

Question by Andrew Butler

I am looking to have my files on my PHP site like so

http://mysite.com/files/file.exe?auth=qwe1245efmkrkn%$!e12 <– some generated hash…

I haven’t written any code, but I was wondering how I would implement the auth variable for a direct link to a file.. any ideas? Thanks!

Answer by Starx

You can do this, better with .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?yourdomain.com [NC]
RewriteRule .(jpg|jpeg|png|gif)$ - [NC,F,L]

Copy and paste this code, rename it with .htaccess and save on the root.

As Gumbo mentioned, its true that some browsers, do not send any request header and forging a request is very easy, unless you apply CSRF protection on your website.
So an updated .htaccess to allow this

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?yourdomain.com/ [NC]
#Redirect to an image
RewriteCond %{REQUEST_URI} !hotlink.(gif|png) [NC]
RewriteRule .*.(gif|jpg|png)$ http://yourdomain.com/images/hotlink.png [NC] 

and adding

Header set X-Frame-Options DENY

ensures the website from being framed, even from the same website. Might be a little helpful against CSRF.

Alternative to this, might be redirecting an image request to a page to handle it.

The font is not being displayed at the bottom of the container?

Question by Beginner

This is my HTML code to display font with a background in a small container . .

here is the css

.cons_save h4{font:bold 22px/60px Arial,Helvetica, sans-serif;margin:20px 0px -15px 20px; color:#f78d1d;vertical-align: bottom; background:url(../images/save_bg_cons.png) no-repeat; width:151px; height:69px;}

im not able to put it ! in the bottom of that container whatever the other content above it in the container remains

My HTML CODE

<div id ="<?php echo $store->branch_id;?>Collect" style="display:block">    
 <span class="cons_save fl clr">
  <h4><?php echo $save. " %"; ?> </h4>
 </span>
</div> 

Answer by Starx

Don’t put heading tags insides spans

<div id ="<?php echo $store->branch_id;?>Collect" style="display:block">    
     <div class="cons_save fl clr">
        <h4><?php echo $save. " %"; ?> </h4>
     </div>
</div>

And also heading having a lot of margin & padding by default. So might be the reason to come in the bottom. Be sure to clear it

.cons_save h4 { margin: 0;padding:0; }

It works without the reset too, check here

how do you find the non-computed css width with JQuery

Question by coder

I know I can use .width() and .css('width') to get the computed width of an element, but how do I get the value that is set on the element for the width attribute? This is not the same value as the computed width and I need to know dynamically what the intended width is.

Answer by Starx

style attributes are changed, once you manipulate it using jQuery. So it cannot be retrieved later on. The best way, I think would be backing up at load if you need it.

$(function() {
   var backup = $("#element").attr("style");
   // do others
});

See this demo to get my point.

Later you can use .split() method to extract info like this

sp = backup.split(";");
$.each(sp, function(k,v) {
    var params = v.split(":");
    var att = params[0];
    var val = params[1];
    //alert(att+val);
});

Demo

Difference between normal cookies and Zend_http_cookies in PHP

Question by Kunal

I am learning a Zend Framework. Its going really difficult to me as compare to CodeIgniter. I have a problem of the difference between normal cookies(php) and zend_http_cookies. I am using normal cookies in my Zend application, it works but I want to understand Zend_http_cookies and its pure concept, can anyone tell me this..
thnx in advance.

Answer by Kunal

PHP cookie :

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

In PHP you can set cookie like this :

Example :

setcookie("user", "username", time()+3600);
//For getting cookie write:
echo $_COOKIE["user"];

Zend_Http_Cookie:

Zend_Http_Cookie is a class that represents an HTTP cookie. It provides methods for parsing HTTP response strings, collecting cookies, and easily accessing their properties.

You can instantiate it using :

$cookie = new Zend_Http_Cookie('user',
                               'user demo',
                               '.example.com',
                               time() + 3600,
                               '/path');

A cookie object can be transferred back into a string, using the __toString() magic method. This method will produce a HTTP request “Cookie” header string, showing the cookie’s name and value, and terminated by a semicolon (‘;’). The value will be URL encoded, as expected in a Cookie header:

Example:

// Will print out 'user=user+demo;' :

echo $cookie->__toString();

echo (string) $cookie;

echo $cookie;

For advance you should read zend documentation from framework.zend.com

Answer by Starx

Zend_Http_Cookie is not related to normal cookies. It is a part of Zend_Http_Client and is a class that represents an HTTP cookie.

It provides methods for parsing HTTP response strings, collecting cookies, and easily accessing their properties. It also allows checking if a cookie matches against a specific scenario, IE a request URL, expiration time, secure connection, etc.

Reference

Recursive data retrieval from database in Zend framework similar to CakePHP

Question by waseem

In CakePHP, we can provide recursive option while retrieving data from database, which automatically fetches the data from all the dependent tables.

We can also provide option for recursive e.g recursive=2, which fetches dependent tables of the dependent tables.

How can we achieve the same functionality in Zend framework?

Answer by Starx

If you have defined Table Relationship on your model before, you can use findDependentRowset() to retrieve records from multiple related table. For more details, see this.

But i am not sure of limiting the recursion.

February 28, 2012

jquery – leaving the hover state

Question by Marc

I have 2 div tag. The first contains a table. The second contains some text. When the page is loaded both div have no background-color. When there is an hover on a td of the first div, the second div background-color is set to yellow (see code below). My problem is that when I leave the table with the mouse, the background of the second div stays yellow. I would like the color to be remove on leaving the hover state. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.

My HTML :

​<div id="div-one">
    <table>
        <tr><td>1</td><td>2</td></tr>
        <tr><td>3</td><td>4</td></tr>
        <tr><td>5</td><td>6</td></tr>
        <tr><td>7</td><td>8</td></tr>                   
    </table>
</div>

​<div id="div-two">
    some text
</div>

My JS:

$(document).on("hover", "#div-one td", function() { 

    $("#div-two").css("background-color":"yellow");
});​

Answer by Didier Ghys

Use mouseenter and mouseleave events separately – which is actually a “hover” event:

$(document).on({
    mouseenter: function() {
        $("#div-two").css("background-color", "yellow");
    },
    mouseleave: function() {
        $("#div-two").css("background-color", "");
    }
}, "#div-one td");​

DEMO

Answer by Starx

There is an error, you are using : when defining single property.
When defining a single property you should use ,

// For single property
$("#selector").css("property", "value");

//Define multiple properties
$("#anotherselector").css({
  property1: value1,
  property2: value2,
});

Solution

Use this

$("#div-one td").mouseover(function() {
    $("#div-two").css("background-color","yellow");
}).mouseout(function() {
    $("#div-two").css("background-color","white");
});

Or, if you want to keep on using .on() method

$(document).on("hover", "#div-one td", function() {
    $("#div-two").css("background-color", "yellow");
}).on("mouseout", "#div-one td", function() {
    $("#div-two").css("background-color", "white");
});

Demo

...

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