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);
?>

Tagged with: ,
Posted in javascript, PHP

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.

Posted in Joomla 1.5, PHP

PHP function to convert time from one timezone to another

Hi ,

I found PHP function which is very useful for time  conversion. I got this from http://truelogic.org/wordpress/2011/05/22/php-function-to-convert-time-from-one-timezone-to-another/

Some applications require finding out what the current time would be in a particular timezone. Another similar requirement would be to convert a given date/time value to a different timezone.

PHP has very powerful date handling functions and it makes the above very simple and easy. We have tried to use quite a few readymade timezone conversion classes but either they had a bug or they were too complicated. Hence we came up with our own class. The actual conversion code is only about 4 lines of code.

 

What it does is convert the time string argument from the source timezone into GMT time and then convert into the target timezone. This approach was far simpler than trying to directly convert from source timezone into the target timezone.

 

The first call to function converts 14 Sep, 2011, 02 am from the my timezone GMT+0530hrs to GMT-600hrs
here I am passing my time zone i.e. +5.30— my current time & date in format(2011-0-14 02:00:00)here
the time in 24hours– last parameter to which time zone you want to convert..here I put
the timezone of Texa-USA i.e. -6
After running the program.. I got result as ‘2011-09-14 02:00:00’.

 

<?php

class convert_timezone
{
  function convert_timezone() // Constructor of the class
  { }
  function conver_to_time($conv_fr_zon=0,$conv_fr_time="",$conv_to_zon=0)
  {
   //echo $conv_fr_zon."<br>";
   $cd = strtotime($conv_fr_time); 
   $gmdate = date('Y-m-d H:i:s', mktime(date('H',$cd)-$conv_fr_zon,date('i',$cd),date('s',$cd),date('m',$cd),date('d',$cd),date('Y',$cd)));
   //echo $gmdate."<br>";
   $gm_timestamp = strtotime($gmdate);
   $finaldate = date('Y-m-d H:i:s', mktime(date('H',$gm_timestamp )+$conv_to_zon,date('i',$gm_timestamp ),date('s',$gm_timestamp ),date('m',$gm_timestamp ),date('d',$gm_timestamp ),date('Y',$gm_timestamp ))); 
   return $finaldate;
  }
}

$resultTime = $c->conver_to_time(5.30, "2011-09-14 02:00:00", -6);
echo $resultTime;

?>
Posted in PHP

Joomla offline Site but allow for registered users

When we are developing any new joomla site… we need to make site offline….

we can do this site->global configuration-> Site Settings —  > Site Offline to yes…

but need to open for registered user…

An override (minor hack) to Joomla 1.5 as follows:

In /includes/application.php, change value of default 23 to 18

from:

if ($this->getCfg(‘offline’) && $user->get(‘gid’) < ’23’ ) {

$file = ‘offline’;

to:

if ($this->getCfg(‘offline’) && $user->get(‘gid’) < ’18’ ) {

$file = ‘offline’;

This will enable registered users to view the website after login on the offline page.

This will work for joomla 1.6 also……

Posted in Joomla 1.5

How to change Joomla Admin Folder Name or Path ?

Many times people ask me, how you secure your joomla website,specially how to protect your admin side of website.So I think about to change the admin url.
By default, every joomla programmer know the default url of admin directory i.e. http://sitename.com/administrator/ .

So I want to hide or just don’t want to give direct access to that url, after googling, found that changing the folder name ‘administrator’ to ‘myadmin” & change the ,

define( ‘JPATH_ADMINISTRATOR’, JPATH_ROOT.DS.’administrator’ );  to

define( ‘JPATH_ADMINISTRATOR’, JPATH_ROOT.DS.’myadmin’ );

in the ‘includes->defines.php’ & ‘administrator->includes->defines.php’ doesn create some problem in thired party component.

So finally I found another trick which work for me, is as below.

1. Create a new directory in your root directory (eg. “myadmin”)
2. Create an index.php file in your “myadmin” directory..

<?php
$admin_cookie_code=”999999999″;
setcookie(“JoomlaAdminSession”,$admin_cookie_code,0,”/”);
header(“Location: ../administrator/index.php”);
?>

3. Add this to the beginning of index.php in real administrator folder
#administrator/index.php (modify , **do not replace**)
if ($_COOKIE[‘JoomlaAdminSession’] != “999999999”)
{
header(“Location: ../index.php”);
}
This works for me, I hope it will help others also.
Posted in Joomla 1.5

Joomla -redirect non-www URLs to www

In order to redirect all of the requests for yourdomain.com to http://www.yourdomain.com, you should set the appropriate rewrite rule. This can be done by adding the following lines at the beginning of the .htaccess file in your WWW folder:

RewriteCond %{HTTPS} !=on

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

From now on, when someone accesses http://yourdomain.com s/he will be redirected to http://www.yourdomain.com.

Using this method is safe because it should not create any redirection loops nor it should interfere with other rewrite rules.

In Joomla 1.5, we have to add above rule (code) in .htaccess file, before the joomla .htaccess rule.

so your .htaccess file look like this….

##  Can be commented out if causes errors, see notes above.

Options +FollowSymLinks
##  mod_rewrite in use
RewriteEngine On

#Custom rule(It must be before the joomla htaccess rule) for non www to www url:Start

RewriteCond %{HTTPS} !=on

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

#Custom rule for non www to www url:End
########## Begin – Rewrite rules to block out some common exploits

Tagged with: ,
Posted in .htaccess, Joomla 1.5

How to enable error reporting in php

How to enable error reporting in php

This article , I copied from http://qualitypoint.blogspot.com/2009/07/how-to-enable-error-reporting-in-php.html

In Shared Web hosting, normally we won’t be having access to the root folder php.ini file for changing any settings.

Some settings can be changed by putting another php.ini in our required folder. In this case the value will be taken from php.ini located in our folder instead of from the root folder.

But it will not be applicable for all the settings. Some settings can not be overridden.

Say for example, display of error message in the browser will be disabled by default in all production environments. But it seems putting this setting should be changed only in the php.ini file available in root folder.

But we may need to see the error message if our webpage is not working properly. We can not ask the shared web hosting company to turn on the error message. Because it will affect other people websites also.

To avoid this issue, the below code can be placed in the php file for enabling the error message display for the particular page.

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

/**Or you can write code as below**/

if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set(‘display_errors’,’On’);

} else {
error_reporting(E_ALL);
ini_set(‘display_errors’,’Off’);
ini_set(‘log_errors’, ‘On’);
ini_set(‘error_log’, ROOT.DS.’tmp’.DS.’logs’.DS.’error.log’);
}


?>

Posted in PHP