Archive for February, 2011


Magento – Fix for admin login problem

Out of all CMS Magento installation is really big for everyone. Some time works and other time not works. Its all due to lake of common tips we missing while installation or After installation.

To make your mangeto admin panel working after installation follow below steps.

1] modify the core Magento code (keep in mind you may have to re-apply the fix if you update the code Magento code) – go to: app/code/core/Mage/Core/Model/Session/Abstract and open up varien.php, and comment out lines 73 (comment out the comma at the end of the line) through 76 so that it looks like the following:

// set session cookie params
session_set_cookie_params(
$this->getCookie()->getLifetime(),
$this->getCookie()->getPath() //remove this after putting on server (leave the comma)   ,
//$this->getCookie()->getDomain(),
//$this->getCookie()->isSecure(),
//$this->getCookie()->getHttponly()
);

For Latest version of magneto you have to do something new :)

In the latest version of which i’m using is 1.4.0.1
in that the below mentioned code is not available

app\code\core\Mage\Core\Model\Session\Abstract\varien.php

$this->getCookie()->getDomain(),
$this->getCookie()->isSecure(),
$this->getCookie()->getHttponly()

Comment these lines line (may be at line 78…);

search for “// session cookie params” line in app\code\core\Mage\Core\Model\Session\Abstract\varien.php

change

$cookieParams = array(
‘lifetime’ => $cookie->getLifetime(),
‘path’ => $cookie->getPath(),
‘domain’ => $cookie->getConfigDomain(),
‘cookieParams’ => $cookie->isSecure(),
‘httponly’ => $cookie->getHttponly()
);

to

$cookieParams = array(
‘lifetime’ => $cookie->getLifetime(),
‘path’ => $cookie->getPath()
);

and comment the below mentioned code
line number 86,

/* if (!$cookieParams['httponly']) {
unset($cookieParams['httponly']);
if (!$cookieParams['secure']) {
unset($cookieParams['secure']);
if (!$cookieParams['domain']) {
unset($cookieParams['domain']);
}
}
}

if (isset($cookieParams['domain'])) {
$cookieParams['domain'] = $cookie->getDomain();
}
*/

now login as admin, it will work
Hope to get lots of thanks here :)

Magento installation problem in localhost – [validate-url] – Please enter a valid URL. Protocol is required…

Comment line no. #501 in \magento-1.4.2.0\js\prototype\validation.js file by code below:
like below i did

['validate-url', 'Please enter a valid URL. Protocol is required (http://, https:// or ftp://)', function (v) {
//return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
return true;
}],

it will works :)

Insert Your Recent Comments from Facebook to Database

The sample is for scraping data from Facebook home page. Once you will pass your username and password in variables  i have declared in file and execute the code you will get your home page of Facebook.

Here php code will pass the variables by POST method to https://login.facebook.com/login.php page and login into facebook account then other curl call will call my home page to get the home page HTML.

I have written code of how i am scrapping data by DOM Parser.

<?PHP
$first_name = 'First Name';
$login_email = 'facebookloginemail';
$login_pass = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
$page = curl_exec($ch);

/*
Here it will print the facebook home page with your recent updates
*/

echo $page;

// Parse the HTML information and return the results.
$dom = new DOMDocument();
@$dom->loadHtml($page);

$xpath = new DOMXPath($dom);

// Get a list of articles from the section page

$articleList = $xpath->query("*/div[@id='m_stream_stories']");
print_r($articleList);

// Add each articles url to the Articles array
// This way we can scrap any part of data of any sites.

$articles = array();
foreach ($articleList as $item){
	$articles[] = array(
								'url' => $item->getElementsByTagName('a')->item(0)->getAttribute('href')
					   );
}

print_r($articles);

// Here we can loop for the article array and insert it into database.
?>

If you have any trouble in getting it work let me know i will help you.

Update Your Facebook Status By PHP Script

Hey would you like to update your facebook status, but don’t want to go to facebook.com, login, and finally being able to update your status?
Then Do one thing simply save the following code on your server, define the variables and Rock :)

<?PHP
/*******************************
*	Facebook Status Updater
*	Christian Flickinger
*	http://nexdot.net/blog
*	April 20, 2007
*******************************/

$status = 'YOUR_STATUS';
$first_name = 'YOUR_FIRST_NAME';
$login_email = 'YOUR_LOGIN_EMAIL';
$login_pass = 'YOUR_PASSWORD';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
$page = curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 1);
preg_match('/name="post_form_id" value="(.*)" \/>'.ucfirst($first_name).'/', $page, $form_id);
curl_setopt($ch, CURLOPT_POSTFIELDS,'post_form_id='.$form_id[1].'&status='.urlencode($status).'&update=Update');
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
curl_exec($ch);
?>

source : http://codesnippets.joyent.com/posts/show/1204

Get Safe File Name

This function come up with a function that does a good job of sanitizing certain strings so that they are safe to use in the URL (like a post slug) and also safe to use as file names. For example, when someone uploads a file I want to make sure that I remove all dangerous characters from the name.

 function filename_safe($filename) {
     $temp = $filename;
     // Lower case
     $temp = strtolower($temp);
     // Replace spaces with a '_'
     $temp = str_replace(" ", "_", $temp);
     // Loop through string
     $result = '';
     for ($i=0; $i<strlen($temp); $i++) {
         if (preg_match('([0-9]|[a-z]|_)', $temp[$i])) {
             $result = $result . $temp[$i];
         }
     }
     // Return filename
     return $result;
 }

Let me know if you have any queries in using the functions.

Uploading file by PHP Code

Let’s start by creating the html file form:

Files are uploaded from the browser using an input tag, with the type parameter set to “file”. This is supported by all browsers currently available on the market.

The most important thing is to set the ENCTYPE attribute of the form to “multipart/form-data” and set the form’s action to the file upload page. The file upload page will handle the actual file uploading.
html file : upload.html

 <form name="frmupload" id="frmupload" method="post" action="/upload.php" enctype="multipart/form-data">
  <fieldset>
  <legend>File upload:</legend>
  <table>
  <tr>
	<td>
	  <label for="file">File:</label>
	</td>
	<td>
	  <input name="vFile" type="file" id="vFile" />
	</td>
  </tr>
  </table>
  <input type="submit" name="upload" value="Upload" />
  </fieldset>
</form>

Now first create the upload script:
In PHP, uploaded files are accessed via the $_FILES array:

* $_FILES["file"]["name"]: The original filename on the client’s machine.
* $_FILES['file']['type']: The mime type of the file.
* $_FILES['file']['size']: The size, in bytes, of the uploaded file.
* $_FILES['file']['tmp_name']: The temporary filename of the file in which the uploaded file was stored on the server.

Here i have created one function so we will call this in our upload.php it will makes easy to upload , optimize the code and make the code reusable.

php script file : upload.php

<?php
$imagepath='';		// Make Sure you have set the permission of folder in which you are uploading files.
$image= $_FILES['file']['tmp_name'];
$image_name= $_FILES['file']['name'];
$prefix=''; 	// Optional
$vaildext= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png"; 

$output = fileupload($imagepath , $image , $image_name , $prefix , $vaildext);

fileupload($imagepath,$image,$image_name,$prefix='', $vaildExt= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png");
 function fileupload($imagepath,$image,$image_name,$prefix='', $vaildext= "gif,jpg,jpeg,bmp,PNG,JPG,JPEG,GIF,BMP,png")
	{

	   $msg="";
	   if(!empty($image_name) and is_file($image) )
		{
			$tmp=explode(".",$image_name);
			for($i=0;$i<count($tmp)-1;$i++)
			{
				$tmp1[]=$tmp[$i];
			}
			$file=implode("_",$tmp1);
			$ext=$tmp[count($tmp)-1];

			$vaildext_arr = explode(",", strtoupper($vaildext));
			if(in_array(strtoupper($ext), $vaildext_arr))
			{
				$imagefile=$prefix.$file."_".date("YmdHis").".".$ext;
				$ftppath1 = $imagepath.$imagefile;
				if(!copy($image, $ftppath1))
				{
					$imagefile = '';
					$msg="Failed to Upload files !!";
				}
				else
					$msg="File uploaded successfully !!";
			}
			else
			{
				$imagefile = '';
				$msg="Extension Is Not Valid, please use mentioned extesions only $vaildExt !!!";
			}
		}
		$ret[0] = $imagefile;
		$ret[1] = $msg;
		return $ret;
	}
?>

Its easy and working :)

Let me know your thoughts if you are facing any problem while uploading files.

source:stackoverflow.com
Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories