Freedom To Learn ……….

Writing, to me, is simply thinking through my fingers. — Isaac Asimov


Leave a comment

Yii Tips and Tricks

While working on Project base on Yii framework, got some questions/problems. After searching couple of hours found solutions,So sharing here….,

  • How can change default action of Yii controller?

Ans:- Declare variable  ‘ public $defaultAction = ‘actioname’; ” in controller.
e.g.:-  class LoginController extends Controller
{             public $defaultAction = ‘login’;

  • How to change the default Controller of site in Yii?

Ans:- add ” ‘defaultController’=>’module/controller/action ”  in main.config

  • How to change the default theme of site?

    Ans:- add ” ‘theme’=>’theme-name ”  in main.config

  • How do I create a linked image or CHtml:link with an image?

    Ans:-
    $imghtml=CHtml::image(Yii::app()->theme->baseUrl.’/images/settings-icon.png’);
    echo CHtml::link($imghtml,Yii::app()->createAbsoluteUrl(‘/controller/action/’),array(“class”=>”classname”));

  • How to change the layout in contrlller?

    call  $this->layout = “//layouts/name” if you want change on particular action, or declare variable as,
    public $layout = “//layouts/name”;

  • How to set the time zone in Yii?

 We can set the time zone index.php using php function ,       date_default_timezone_set(‘Asia/Calcutta’);
But I found another way,
Open main.php & write as key =>pair value like,

return array(
‘timeZone’ => ‘Asia/Calcutta’,
……………….
……..
);


1 Comment

URL rewriting in Yii to hide index.php

Hi Folks,

I am using Yii -yes it is,  MVC framework for new project in PHP.
I started  learning  yii from the official site of yii. I found it is hard to start the work on yii, specially who don’t have
knowledge of MVC framework like Code Igniter (CI) or Zend. It take some time to understand the MVC in yii, as nobody of  us is familler with CI or zend.

Finally we figure out the basics of  Yii. While working on basic of Yii, I want to rewrite the url to SEO friendly.
So I started to search on google, forum got useful information here http://www.yiiframework.com/doc/guide/1.1/en/topics.url

To hide the index.php from url I did changes in config/main.php  as shown below,

‘urlManager’=>array(
                              ‘urlFormat’=>’path’,
                              ‘showScriptName’=>false,
                              ‘rules’=>array(
                                                ‘<controller:\w+>/<id:\d+>’=>’<controller>/view’,
                                               ‘<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’,
                                              ‘<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,
                                             ),
), 

and I created new .htaccess file in the same directory as my index.php file shown as below,

RewriteEngine  On
RewriteBase  /
RewriteCond   %{REQUEST_FILENAME} !-f
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteRule    ^(.*)\?*$ index.php/$1 [L,QSA]

 You can change the ”RewriteBase /’  as per project location like if project is located in ‘folder1′ of webroot.
then it change like as    RewriteBase  /folder1

Note that my urlManager has the line:

‘showScriptName’=>false,

That will make sure your auto-generated links (CHtml::link or zii.widgets.CMenu) do not have index.php in them. This will not work without the .htaccess file. The reason is that the .htaccess file rewrites the url to point to index.php/$1 so that Yii actually gets the request properly. You will not see the index.php in the url at any time though.

Yii hide index.php from url, geting 404 error

Now it’s time to update this post, While working with Cest Os in local network, I did same thing to rewrite the url.
but it don’t work, the apache rewrite modue is enabled, all things are perfect ,
but still I am geting 404 error while while accessing the page.
After googling couple hours , I found the I have problem in ‘httpd.conf’

Options Indexes FollowSymLinks
#this is the part that needs changing. I changed ‘None’ to ‘All’
AllowOverride All
Order allow,deny
allow from all

this allows virtual host rewrite index.


Leave a comment

Include php file On all the pages of php

I need to include a files in all of your site’s pages? Normally, you’d use require() or include(). But do I really want to manually past the include code in every page?

This may be at the start of page or end of page.

There’s the little known PHP directive auto_prepend_file and auto_append_file. What are PHP directives? They’re essential global PHP settings that are defined in you php.ini file. There is surprisingly little information about these methods out there, but luckily they aren’t too difficult to work with.

In the php.ini file, we can implement like this,

; Automatically add files before or after any PHP document.
auto_prepend_file = D:/wamp/www/includefileatstart.php
auto_append_file =  D:/wamp/www/includefileatend.php

For Linux system it is something like this

/home/username/public_html/includefile.php

For me this will not work on Wamp…  ;)

So I have to find another way, … so we did with the help of .htaccess.. we can set it at runtime…

Create .htaccess file in www folder.

php_value auto_prepend_file D:/wamp/www/includefileatstart.php
php_value auto_append_file D:/wamp/www/includefileatend.php

For Linux system it is something like this

/home/username/public_html/includefile.php

Create file the file which you want to include  ‘includefileatstart.php’ and ‘includefileatend.php’

includefileatstart.php

<?php

echo “included at the start of program <br />”;

?>

includefileatend.php

<?php

echo “included at the end of program<br />”;

?>

Create test file:-  test.php

<?php

echo “this is body of page<br>”;

?>
You can download here… http://code.google.com/p/project-php-pretend-append-file/downloads/list

I have created function file & this is included at the start.
Like this :- includefunction.php

<?php
function pa($a,$d=0)
{
echo “<pre>”;
print_r($a);
echo “</pre>”;
if($d==1) die;
}
?>
Tested like this, test2.php
<?php
$array=array(’1′,’2′,’3′,’4′,’5′,’6′);

pa($array);

//pa($array,1);

?>

this will o/p the array.


Leave a comment

PHP security tips

NEVER trust user input

User the following validation methods for input.

<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
// Query database to check if there are any matching users
$query = “SELECT * FROM users WHERE user=’$username’ AND   password=’$password’”;
mysql_query($query);

$variable = htmlentities($_GET['page'], ENT_QUOTES, ‘UTF-8′);
// or even more secure
$variable = strip_tags($_GET['page']);
?>


3 Comments

Joomla1.5 manually add admin user

Hi ,…
When we work with joomla site… we should have the back end access.. i.e.  super-admin access.

But we don’t have that…. may be client may forgot to give you or given wrong details…

So need to create new one.. or just change the password in database of joomla site.

We  faced the situation where client has given the correct FTP details… but wrong admin details.. & clinet want some urgent changes on the site.. .. so we have just created simple script in PHP .. which create new User for your joomla using PHP & mysql….

<?php
	error_reporting(-1);
	ini_set("display_error",1);	
?>

<html>
<head>
<title>Joomla1.5 User Creater</title>
<style type="text/css">
#user{	
	margin:0 auto;
	width:500px;	
}
#user td{
	height:30px;
}
h1,h2{
	text-align:center;
}
</style>
</head>
<body>
<form name="joomadduser" method="POST">
	<table cellspacing="0" cellpadding="0"  id="user">
	
<?php
	
if(isset($_POST['submit']))
{			
	$host=trim($_POST['hostname']);
	$dbuser=trim($_POST['dbusername']);
	$dbpassword=trim($_POST['dbpassword']);
	$db=trim($_POST['dbname']);
	$tbprefix=trim($_POST['tbprefix']);	
	
	/****Custom Username & Password*****/
	$username=trim($_POST['username']);
	$password=md5(trim($_POST['password']));	
	$email=trim($_POST['email']);
	
	/********User Creation time********/
	$registerDate=@date('Y-m-d H:i:s');
		
	$con=mysql_connect($host,$dbuser,$dbpassword) or die(mysql_error().'Sql error , Connection failed');
	if($con)
	{
		/***insert admin user in table ***/
		mysql_select_db($db) or die(mysql_error()."Not able to connect to Database");
		$sql="insert INTO ".$tbprefix."users (name,username,email,password,usertype,block,sendEmail,gid,registerDate,lastvisitDate,activation,params) "."values ('$username','$username','$email','$password','Super Administrator',0,1,25,'$registerDate','$registerDate','','')";	
		
		mysql_query($sql) or die(mysql_error()."Not able to create admin");
		$adminid=mysql_insert_id($con);				
		
		/***insert admin user id  into usergroup section table ***/		
		$query="insert into ".$tbprefix."core_acl_aro (section_value,value,order_value,name,hidden) VALUES ('users','$adminid',0,'$username',0)";
		mysql_query($query) or die(mysql_error()."Not able to add in section");
		$sectionid=mysql_insert_id($con);
		
		/***insert admin user id  into usergroup ***/
		$query="insert into ".$tbprefix."core_acl_groups_aro_map VALUES (25,'',$sectionid)";
		if(mysql_query($query)  or die(mysql_error()."Not able to add in group"))
		{
			echo "<tr><td align='center'><h1>New User Created Succesfully.</h1><h2>Details As Below:</h2>";	
			echo "Username:-".$username."<br \>";	
			echo "Password:-".trim($_POST['dbpassword'])."</td></tr>";				
		}		
	}
}	
else
{	
?>

		<tr>
			<td colspan="2"><h1>Joomla1.5 manually add admin user</h1></td>			
		</tr>
		<tr>
			<td colspan="2" height="35"></td>
		</tr>
		<tr>
			<td colspan="2" height="35"><h2>Enter Database Details: </h2></td>			
		</tr>
		<tr>
			<td>Host Name</td>
			<td><input value="" name="hostname"></td>
		</tr>		
		<tr>
			<td>Database Username</td>
			<td><input value="" name="dbusername" id="dbusername" value=""></td>
		</tr>
		<tr>
			<td>Database Password</td>
			<td><input type="text" name="dbpassword" value=""></td>
		</tr>
		<tr>
			<td>Database Name</td>
			<td><input value="" name="dbname" id="dbname" value=""></td>
		</tr>
		<tr>
			<td>Table Prefix</td>
			<td><input value="" name="tbprefix" value="jos_"></td>
		</tr>
		<tr>
			<td colspan="2"></td>			
		</tr>
		
		<tr>
			<td colspan="2" height="35"><h2>Enter User Details: </h2></td>			
		</tr>				
		<tr>
			<td>New Joomla Username</td>
			<td><input value="" name="username" value=""></td>
		</tr>
		<tr>
			<td>New Joomla Password</td>
			<td><input type="text" name="password" value=""></td>
		</tr>
		<tr>
			<td>Email Id</td>
			<td><input type="text" name="email" value=""></td>
		</tr>
		<tr><td colspan="2"></td></tr>
		<tr>
			<td></td>
			<td><input type="submit" name="submit" value="Submit"></td>
		</tr>
<?php
}
?>
</table>
	</form>
</body>
</html>


Leave a comment

Doing page redirection in PHP

Hi,

In php header() function is used to redirect page.
Syntax:-
header(“Location:newpage.php”);

header() must be used before sending any output either by HTML, blank lines or from PHP.
Otherwise error message “header already sent” error shown.

To overcome this problem, we can use  javascript for doing page redirection as below

window.location=”newpage.php”;

But if the JavaScript is disabled by the browser, then it will not work.

So we need can write a function in PHP like below,

<?php
function redirect($url=””)
{
if (!headers_sent()) header(“Location:$url”);
else
echo “<script>window.location=\”$url\”; </script>
<noscript>Automatic redirection didn’t work.<br />
<a href=\”$url\”>Click here to go to newpage.</a> </noscript>”;
}
$url=’newpage.com’;
redirect($url);
?>


2 Comments

Users Sharing or Synchronisation between multiple Joomla sites

Hi..
my client have two websites.. he want user of one website can access the other site too..

I have searched on google  .. I found something interesting & useful…

http://forum.joomla.org/viewtopic.php?p=2015114&sid=d5b6fe1ecd7a5fbda2417709d3d85818#p2015114


******   ***
I managed to make two sites work with common user base, I decided to share how it should be done step by step.

What is the solution?

  • Allows two or more Joomla! sites to have one and the same user base and sessions.
  • It could be used for other stuff (i.e. banners, sections etc.) however this is not subject of this guide. We will review only users and sessions.

What the solution is not?

  • It is not a bridge, a component, module, plugin or something like this. This is a solution on database level.
  • If you want to share components, articles etc. it is much more complicated, however not impossible. But this is not something we will review here.
  • It is not recommended to use this solution for two already existing sites, as at least one of them will either crash, or you will at least lose its user base.
  • The users in the two (or more) sites will have one and the same access rights (i.e. super admin in one will be super admin in the others as well and so on, you cannot set different rights for the different sites).

Requirements:

  • MySQL 5
  • PHP 5
  • Joomla! 1.5
  • The sites should share one and the same database server and the database user should have access rights to all databases in use.

BEFORE DOING ANYTHING TO YOUR EXISTING WEB SITES, MAKE A BACKUP, AS YOU MAY LOSE DATA.

Step by step:

  • Install Joomla! on localhost/site1 (or domain.com – doesn’t matter) with database name site1.
  • Install second Joomla! on localhost/site2 (or any domain) with database name site2 – it is important that the database user for both sites is one and the same.
  • Open the configuration.php files of both installations and copy the secret value ($secret = ‘somesymbolshere’;) from the configuration of site1 to the configuration to site2. So both installations should have one and the same secret key in their configuration. If you want to add even more sites, the other sites should also have the same key for secret.
  • Open phpMyAdmin of the database of site2 and execute one by one (each row is one execution) the following:
Code:
DROP TABLE `jos_users`
DROP TABLE `jos_session`
DROP TABLE `jos_stats_agents`
DROP TABLE `jos_core_acl_aro`
DROP TABLE `jos_core_acl_aro_groups`
DROP TABLE `jos_core_acl_aro_map`
DROP TABLE `jos_core_acl_sections`
DROP TABLE `jos_core_acl_groups_aro_map`

Now run the following SQL queries in the same database (if necessary replace site1 with the corresponding database name used by site1)

Code:
CREATE VIEW jos_users AS SELECT * FROM site1.jos_users
CREATE VIEW jos_session AS SELECT * FROM site1.jos_session
CREATE VIEW jos_stats_agents AS SELECT * FROM site1.jos_stats_agents
CREATE VIEW jos_core_acl_aro AS SELECT * FROM site1.jos_core_acl_aro
CREATE VIEW jos_core_acl_aro_groups AS SELECT * FROM site1.jos_core_acl_aro_groups
CREATE VIEW jos_core_acl_aro_map AS SELECT * FROM site1.jos_core_acl_aro_map
CREATE VIEW jos_core_acl_aro_sections AS SELECT * FROM site1.jos_core_acl_aro_sections
CREATE VIEW jos_core_acl_groups_aro_map AS SELECT * FROM site1.jos_core_acl_groups_aro_map

And that is all. Both sites will now have one and the same users and sessions. Adding more sites is easy, just follow the instructions here for site2 database and configuration file.
If you have existing site and you want the new one you are setting up to have all of the users of the old one, you can do it, by just imaging that your old site is site1 in this example and follow the instructions post installation.

If you have two existing sites – THIS IS NOT THE RIGHT SOLUTION FOR YOU.

Updating Joomla?
As for versions 1.5 – there is no problem. Upgrade both of your installations without any worries.
As for version 1.6 – it is hard to say yet, as it is not stable enough.

Hope you find it useful. If you have any questions, ask in that thread.

Follow

Get every new post delivered to your Inbox.