[HowTo] Stop phpmyadmin from asking username, password in localhost

Yesterday, I showed you how you can keep local installation of phpmyadmin from logging you out every few minutes. I took one more step and edited configuration to not ask me username, password at all. Now phpmyadmin never asks me for mysql username and password.

Keep in mind if you follow this tutorial anyone would be able to access your database using phpmyadmin unless you deny access via apache configuration. Don’t do this on production servers.

Steps to follow:

  • Run: gksudo gedit /etc/phpmyadmin/config.inc.php
  • Find:
    $cfg['Servers'][$i]['auth_type'] = 'cookie';

    And change it to:

    $cfg['Servers'][$i]['auth_type'] = 'config';
  • Just below this line, add:
    $cfg['Servers'][$i]['user'] = '';
    $cfg['Servers'][$i]['password'] = '';

    Replace and with mysql username and password.

  • Save the file.
  • Changes will be applied immediately, no need to restart apache.

Now, you’ll never have to enter username and password in phpmyadmin.

Cheers!

[HowTo] Extend phpmyadmin session expiration time

Recently, I got frustrated being logged out every few minutes (24 minutes, to be precise) from phpmyadmin on my localhost. I make some db changes, write some code and when I get back to phpmyadmin, I would see my session had expired. To fix this, I changed the configuration so that my session would expire only after 24 hours of inactivity.

Steps to follow:

  • Copy the following code at the end of /etc/phpmyadmin/config.inc.php:
    $cfg['LoginCookieValidity'] = 60*60*24;
    ini_set('session.gc_maxlifetime', $cfg['LoginCookieValidity']);

This will let your session expire only after 60*60*24 seconds, that is one day.

Cheers!

Passing variable number of arguments to a function in PHP

We all know about functions and how we can pass arguments to it. But did you know that we can pass variable number of arguments in a function?

Consider a function sum() which adds numbers passed into its arguments. Now we want it to add all the numbers passed onto the arguments. Eg; if we call sum (21, 45) or sum (45, 23, 78, 56, 90) it should add all of them. Even if we pass a 100 arguments, it should execute successfully.

Below is the code for making the function sum() :

function sum() {
	$count = func_num_args();
	 $sum = 0;
	 for($i=0; $i<$count; $i++) {
		 $num = func_get_arg($i);
		 $sum += $num;
	 }
	 return $sum;
}

In above function func_num_args() give the total number of arguments that have been passed and func_get_arg() takes argument number as argument and returns the argument value.
We also have another function – func_get_args() which returns all the arguments passed to the function as an array.

For more information and examples on these functions:

func_num_args()
func_get_arg()
func_get_args()

Cheers!

CSS4, What’s coming

About CSS specification

The CSS team recently published first commercial working draft of CSS4. The CSS spec changed just after the release of CSS3. As we know CSS3 is making its way slowly and suddenly W3C launched first official draft of CSS4. So, CSS3 and CSS4 may go hand in hand. Demand for new module makes CSS4 to start their release early. Selector module is an example of it.

New things about CSS4

Pseudo-elements

The first thing we can notice in CSS4 is Pseudo-elements are absent. Don’t worry though, pseudo elements will be there in the future releases of CSS4. New spec might come into different module, or they can merge it into previous modules itself.

UI state pseudo-classes

This class allows you to implement style elements depending on their state and checked items like radio elements, checkbox elements and half a dozen more.

Determining the subject of a selector

In CSS3, if single selector comprises of several other compound selectors, then the last one is considered as its subject. The main function of subject is to determining rules to the elements. But in CSS4 you can make any compound selector as a subject. This is the biggest news in a stream breeze of CSS 4.

The nth-match function

The nth-match() function gives you a freedom to apply a rule to n-th match to the given selectors.

e.g :nth-match(an+b of selector-list)

In early versions, we could only able to give rules to n-th child only. With CSS4 we have more flexibility.

Styling the links

In CSS 4 we have newly designed pseudo class :local-link which allows us to style links based on domain. For example we could actually style each internal link with the help oflocal-link(0).

Reference combinators

These combinators allow us to select elements that are referenced by ID of another element. With the help of different types of combinators that are present in CSS4 we can reference to corresponding element easily.

Reference element pseudo-class ‘:scope’

We can use selectors to query for elements, and then search starts by means of iterator searching one element at a time. And ‘:scope’ is a holder of current searched element.

The :matches()Selector

With the help of :matches selector class we can greatly reduce the complexity level of writing nested CSS. The syntax of :matches()eliminates the need to write outarticle h1,section h1 and many more. We can also use this selector in simple nesting examples but its handy to use it in selecting element that are two or three level deep.

Summary

Above are some specifications that come with the release of first official draft of CSS4. Above specifications were absent in CSS3. With release of first draft and having lot many changes we can expect much more changes in future releases of CSS4. The CSS 4 spec is brand new and no web browser will support this spec. whatever may be the next release whether it is styling or coding, it’s always been helpful for CSS users. This is case whether you endorse a new standard or not.

 

About the author:

Brianne is a blogger by profession. She loves writing on environment and technology. Beside this she is fond of books. She recently did an article on Windows 8 tablet pc. These days she is busy in writing an article on backyard design ideas.

[HowTo] Remove admin bar from wordpress 3.1

The new admin bar in wordpress 3.1 is one good feature to have but sometimes its unwanted. For example you might already have some other kind of top bar with which admin bar may interfere. You can remove the admin bar by putting the following code in /wp-content/YOURTHEME/functions.php:

if(function_exists('show_admin_bar')){
	show_admin_bar(false);
}

[SOLVED] ‘Error at offset’ error in unserialize()

I came across this weird error in unserialize() where it refused to unserialize the serialized data. After hours of trying different things to solve it, finally converting the encoding worked.

The solution to the problem is to use the following function instead of unserialize():

function safe_unserialize($str){
       return unserialize( trim( mb_convert_encoding( $str,'auto','UTF-8' ) ) );
}

Cheers!

[HOWTO] Fix Twitter clients (or PHP scripts) which use basic authentication.

As you probably know, Twitter completely ditched basic authentication method on August 31st, 2010. It now supports only OAuth method of authentication which complicates the coding process multiple folds. Scripts which were previously 20 lines long now span several files.

While there are numerous advantage of Oauth like client doesn’t need to know your password, increased security etc, lots of twitter clients which used basic authentication stopped working right away after Aug 31st.

However, there had to be someone to the rescue. This time it’s http://www.supertweet.net/. They act like a proxy between twitter and your app, converting all basic authentication requests to oauth requests. So practically, twitter will be getting oath requests and your app would still be making basic authentication requests. This would help the twitter clients or php scripts to still use the same code and function just like before.

Documentation:
http://www.supertweet.net/about/documentation
The process is pretty simple and you should get through everything without a hitch.

You just have to change the API url from http://api.twitter.com/ from the application settings or PHP code to http://api.supertweet.net/.

If you still have problems, post it in comments.

Cheers!

[HOWTO] Send emails(using mail function) from localhost in PHP through msmtp (using gmail account) on Linux

There’ve been lots of times when I wanted to use the mail() function on my local server. I am sure lots of you would’ve been wanting it too but most of you would’ve settled for PHPMailer or just used a web host to test the code instead. I myself had been doing the same until recently when I finally decided to do some research and get it to work.

Here are the steps in short for the geeks who like to do things on their own:
All I did was used smtp client called msmtp, configured it to work with my gmail account and configured PHP to use msmtp to send emails.

This tutorial is only applicable for linux users. I’ll write another article for windows users soon when I get my hands on a windows box.
All the commands used in the instructions are for Ubuntu, however you may use corresponding commands for your distro (for eg; you can use yum install instead of apt-get install on fedora, redhat, centos.

Step by step instructions:

  1. First of all, follow this tutorial:
    Install msmtp on your linux box and configure it to work with Gmail.Msmtp is highly configurable and you can easily configure it to work with any smtp server. If you wish to use any other service with msmtp, you may read its manual and configure it.
    Proceed to next step only if you have successfully configured and are able to send a test message.
  2. Open php.ini in your text editor.
    sudo gedit /etc/php5/apache2/php.ini

    Search for ‘sendmail_path’ and change it to look like

    sendmail_path = '/usr/bin/msmtp -t'

    Save the file and exit the text editor.
    Please note that your msmtp path may vary if you are not using Ubuntu. You may find the path to executable by:

    which msmtp
  3. Restart apache:
    sudo /etc/init.d/apache2 restart

    OR

    sudo /opt/lampp/lampp restart
  4. Everything’s done. Lets test if the mail() function is working now:
    if ( mail ( '[email protected]', 'Test mail from localhost', 'Working Fine.' ) );
    echo 'Mail sent';
    else
    echo 'Error. Please check error log.';

    Replace the [email protected] with your own username, save it into a php file along with php delimeters in your virtual host root folder and execute it through the browser. You should receive a mail.

Give yourself a pat on the back.

Some points to note:

  • If you weren’t able to send yourself a test mail in Step 1 ie. setting up msmtp, you should look at the debug information and search for solutions accordingly on the internet. Here’s the manpage of msmtp to help you with configuration
  • If you didn’t receive a mail in your inbox on the last step, check in your Spam folder. If its not there your should check the php error log for hint about what went wrong and act accordingly.
  • For any other questions, suggestions or appreciation feel free to use the comment box.

Cheers!

[SOLVED] ‘Can’t connect to local MySQL server through socket’ error

Yesterday I got the error:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

when I ran the command mysql through terminal in Ubuntu 10.10

This error can also be linked to error in phpmyadmin:

Connection for controluser as defined in your configuration failed.

I figured out it was due to mysqld not running. I simply did

sudo /etc/init.d/mysql start

to start mysql daemon. This solved the error.

[SOLVED]’Error establishing a database connection’ in wordpress

One of my clients got this error in his wordpress:

Error establishing a database connection

It is one of those vague errors which give very little information about what caused it. On going to /wp-admin, it gave error:

One or more database tables are unavailable. The database may need to be repaired.

I added “define(‘WP_ALLOW_REPAIR’, true);” in wp-config.php and clicked on ‘repair’, it showed that all tables have been repaired but I was still getting the error about wordpress not being able to connect with database. It was actually able to connect but there was some other problem. I checked if any table was missing from the database, but it was not.

Finally after 2 hours of searching for a solution, I found it. In wp-options table, just make sure the option_value for the option_name ‘siteurl’ is your site’s url. You can also do this by executing the following sql statement (you can use phpmyadmin):

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Replace ‘YOUR_SITE_URL’ with your site’s url. For eg; for my blog it would be:

UPDATE wp_options SET option_value='https://www.absolutelytech.com' WHERE option_name='siteurl'

That would make the error go away. Please note that this is just one of the solutions to this problem, it may not be solved after following the above steps. In that case, it might’ve been caused due to different reason and you must search for another solution on wordpress forum.

Cheers!