Moodle Error – Incorrect access detected


Have you even got this message. “Incorrect access detected, this server may be accessed only through … address” So how to remove that? Its so simple

Go to moodle/lib/setuplib.php

and change the following file:

Redirect ($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3);

to

Redirect ($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 0);

Now Moodle will redirect immediately to the wwwroot url defined in config.php without any warning message.

Cheers
Jaswant

MySQL – Fetch duplicate records


If your database table having duplicate records and you want to remove all duplicates, this query will help you to find out duplicate records in a table. You can check the records based on any specific field.

SELECT *
FROM mdl_user
WHERE email IN (
	SELECT email
	FROM mdl_user
	GROUP BY email
	HAVING count(email) > 1
	)
ORDER BY email

This query will find all the records in mdl_user table having duplicate emails.

Cheers,
Jaswant

PHP header to prompt file download


Many times I need this function but every time when I try myself I get a new issue, so I thought why not I put it on my blog to help me and other who wanted to prompt a download alert using PHP. I just copied this 😉 from php.net

<?php
function downloadFile($fullPath){

// Must be fresh start
if( headers_sent() )
die(‘Headers Sent’);

// Required for some browsers
if(ini_get(‘zlib.output_compression’))
ini_set(‘zlib.output_compression’, ‘Off’);

// File Exists?
if( file_exists($fullPath) ){

// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts[“extension”]);

// Determine Content Type
switch ($ext) {
case “pdf”: $ctype=”application/pdf”; break;
case “exe”: $ctype=”application/octet-stream”; break;
case “zip”: $ctype=”application/zip”; break;
case “doc”: $ctype=”application/msword”; break;
case “xls”: $ctype=”application/vnd.ms-excel”; break;
case “ppt”: $ctype=”application/vnd.ms-powerpoint”; break;
case “gif”: $ctype=”image/gif”; break;
case “png”: $ctype=”image/png”; break;
case “jpeg”:
case “jpg”: $ctype=”image/jpg”; break;
default: $ctype=”application/force-download”;
}

header(“Pragma: public”); // required
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: private”,false); // required for certain browsers
header(“Content-Type: $ctype”);
header(“Content-Disposition: attachment; filename=\””.basename($fullPath).”\”;” );
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.$fsize);
ob_clean();
flush();
readfile( $fullPath );

} else
die(‘File Not Found’);

}
?>

Drupal CSS optimization failed


My colleague Gaurav he is Drupal expert, is working on a Drupal project. He has almost completed the project so now the time was to switch the project from development to production environment. In the development environment we have Windows Xp, Apache2.2.17, PHP 5.2 and MySQL 5.0.7 and on production environment we have the same PHP, the same Apache, the same MySQL and Windows 2003.

I helped him during server configuration and deployment, everything deployed successfully we were able to see the site. But when he turned on the CSS and JS optimization from admin->performance setting in drupal.

Browser was going nowhere we were just getting the message “Page can not be displayed”. Why we don’t know, because there was no any apache error log, no any php error log. Finally we came to know that CSS optimization is creating an issue, the reason was drupal optimizer was not able to create CSS cache file in sites/default/files/css folder. Whereas JS cache was created in sites/default/files/js folder.

Then we again disabled the CSS optimization, (just only CSS optimization because JS optimization was working very well), now we tried to access the site this time it rendered properly, but only with firefox. Not with IE, this time the reason was clear, because IE only allows 31 CSS request at a time and we were having 34 CSS. We could reduce the CSS requests by disabling few unused modules but it was not a solution to our problem.

Everything was okay but without solving this issue we would not be able to say that everything is working very well. We spent almost two days to find out the reason and solution. Finally I found a solution on Drupal issues page. I found that this is not a strange issue many drupal expert has already faced this. Here is the link to the post

http://drupal.org/node/444228

Somebody has posted a patch for common.inc file which basically does the optimization work, here is the patch

http://drupal.org/node/444228#comment-2095622

So open common.inc file find this code and replace it (offcourse to play safely, first do a backup)


if ($_optimize) {
// Perform some safe CSS optimizations.
$contents = preg_replace('< - \s*([@{}:;,]|\)\s|\s\()\s* | # Remove whitespace around separators, but keep space around parentheses. /\*[^*\\\\]*\*+([^/*][^*]*\*+)*/ | # Remove comments that are not CSS hacks. [\n\r] # Remove line breaks. >x', '\1', $contents);
}

I hope this will help you guys if you work someday on Drupal and face the same issue 🙂 which is very rare.

Cheers,
Jaswant