Category: PHP

php simplexml get a specific item based on the value of a field

January 5, 2014 at 11:59 pmCategory:PHP

Is there a way i can obtain a specific item with SimpleXML ?

For example, i would like to obtain the title of an item having ID set to 12437 with this example xml :

Here are 2 simple ways of doing what you want, one is iterating with each item like this:

Live DEMO.

The other would be using an XPath, to pin point the exact data you want like this:

Live DEMO.

Codeigniter – check if user is logged and exists (it's a real user)

October 27, 2013 at 11:58 pmCategory:PHP

I’m setting a session data for users when they log to my website.

So if the user exists in db i set a session data like : $this->session->set_userdata('user_exists','1');

Now every time i want to check if user exists & is logged i do:

Now i’m wondering if this means that user is logged & exists in db since he logged & i setted him a session param, is this true? Or i’ll obtain security problems?

NB: i’m using session database

thanks

//session encryption is mandatory

Adding custom rewrite rules to WordPress

October 18, 2013 at 10:58 pmCategory:PHP

My WordPress site has a portfolio that is at www.mysite.com/portfolio/. The portfolio sections & items are administered through a custom plugin I created. I want to access the individual items like www.mysite.com/portfolio/my-cool-photo & have that put “my-cool-photo” into a query string like ?portfolio_item=my-cool-photo so I can read it from my code.

In the plugins activation PHP file I have this code:

This adds the rewrite rule to the array OK. The problem is it’s not doing anything. When I go to www.mysite.com/portfolio/testing/ I obtain the “This is somewhat embarrassing, isn’t it?” WordPress 404 error page. Obviously the redirect isn’t working, so the query string won’t be filled, yet just to make sure I did this:

…and sure enough the query string isn’t getting passed.

Is there something I’m missing?

After you update the WordPress rewrite rules, you need to flush them:

http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

You can select to flush with the $hard parameter true, & then you should be able to see your rewrite rules in the .htaccess file.

HTML table with 100% of height, and the rows equally divided in height. How to make it possible?

October 16, 2013 at 10:58 pmCategory:PHP

please go through this fiddle to see what I have tried so far.

I need to display this table occupying full height of the div, & rows of this table should be equal in height to occupy space of full table.
That means, table’s height should be 100% of div’s height.
and each row’s height should be 30% of div’s height.

Any idea of how to achieve this? Also, I would like a solution that should work on most of the browsers, at least, starting from IE 8.

Any assist on this much appreciated.

In styles of the inner div class, alter min-height:100% to height:100% .
That’s all you need!

(This is because min-height can not be inherited)

Here’s the jsfiddle

toString method for SonataAdminBundle Listing in Symfony2

October 8, 2013 at 10:58 pmCategory:PHP

In Symfony 2.3 i am using SonataAdminBundle ( master ) & i am trying to obtain ManyToMany working in Listing. The Problem is that SonataAdminBundle is asking for a toString() method. Implementing this method to the related Entity solves the problem.

My Question: Do i have to implement the toString method or is there a Option to tell SonataAdminBundle a property for using instead of calling the toString method?

Thank you

As far as I know, is mandatory.

But you can return another property value if you want. Also, you can prevent yourself from trying to display a property when the object has no data (for example, when you are “Adding a new object”)

There is a simple way:

PHP 5.3 accessing array key from object getter

October 6, 2013 at 10:58 pmCategory:PHP

I have a Form object $form. One of its variables is a Field object which represents all fields & is an array (e.g $this->field['fieldname']). The getter is $form->fields().

To access a specific field method (to make it required or not for example) I use $form->fields()['fieldname'] which works on localhost with wamp yet on the server throws this error:

I have PHP 5.3 on the server & because I reinstalled wamp & forgot to alter it back to 5.3, wamp runs PHP 5.4. So I guess this is the reason for the error.

How can I access an object method, which returns an array, by the array key with PHP 5.3?

Array dereferencing as described in the question is a feature that was only added in PHP 5.4. PHP 5.3 cannot do this.

So this code will work in PHP 5.4 & higher.

In order to make this work in PHP 5.3, you need to do one of the following:

  1. Use a temporary variable:

  2. Output the fields array as an object property rather than from a method:
    ie this….

    …is perfectly valid.

  3. Or, of course, you could upgrade your server to PHP 5.4. Bear in mind that 5.3 will be declared end-of-life relatively soon, now that 5.5 has been released, so you’ll be wanting to upgrade sooner or after anyway; maybe this is your cue to do? (and don’t worry approximately it; the upgrade path from 5.3 to 5.4 is pretty easy; there’s nothing really that will break, except things that were deprecated anyway)

Nginx Reverse SSL Proxy with PHP

June 26, 2013 at 1:56 pmCategory:nginx | PHP

This trick introduce the way of creating nginx reverse SSL proxy that works with PHP in load balancing environment.

Example network structure

nginx_reverse_proxy

1. Nginx proxy config file for ssl connection
define upsteam in nginx.conf

define the example.com.conf

2. Web server behind the proxy
2.1. Install pecl_http extension with pecl install pecl_http. (This extension allow you to obtain headers from cgi mode, ideally for php-fpm)

2.2. In PHP script, use

To check if the request is from https

Get PHP function body as string

June 14, 2013 at 11:54 amCategory:PHP

This piece of code can return the method body from a class

When to use:

Extract the function details & execute in other another class when the function contains self::func();

Eg:

In class B, we need to call function a from Class A

Output array that contains Unicode characters in JSON format

December 14, 2012 at 11:10 amCategory:PHP

Output array that contains Unicode characters in JSON format (for PHP5.3 or above)

Launch RDP from Web Browser

September 25, 2012 at 11:33 amCategory:PHP

This script allows you create a link that can open a RDP session by passing a server address parameter

eg.

Open RDP