Tuesday, April 24, 2012

Get last inserted row id in Joomla

Ever wanted to get the id of the row that you just inserted to use it for update or any other feature? Well its simple to do it, here is how:

$query = "INSERT INTO #__mytable (name,email) VALUES ('john','john@test.com')";
$db->setQuery( $query );
$db->query();

Once you are done with you insert query simply call the following function:

$lastRowId = $db->insertid();

Works for both Joomla 1.5 and Joomla 2.5

And you will have the inserted row id in the variable. Note that if you remove the $db->query() call from above and then try it will return 0 so just in case if you are wondering it only returns the last value of the table and can have problem if multiple users are working then you don't need to worry as it only returns the id of the row inserted currently.

Edit:
Or if you are using $row->bind() mechanism then you can try the following

$row->bind($data);
$row->store();
$lastRowId = $row->id;

NOTE: You will not get the last inserted row id after you run an update query even though that is not an insertion.

Thursday, April 12, 2012

How to get a URL variable in Joomla

If you want to get a variable from URL the command is simply

$id = JRequest::getVar('id');
It will give you the value of 'id' if its in your url like:

http://www.b4blinky.com/index.php?id=1

However if you don't have an 'id' var in URL it will return you NULL, so if you want to have a default value of let's say zero you can use this


$id = JRequest::getVar('id','0');

Especially useful when you want to have some default views etc.

Monday, April 9, 2012

HTTP to HTTPS in PHP not working solutions

While searching for making your URL from http to secured https you will find dozens of links telling you to write the following:


if($_SERVER['HTTPS']!="on")
  {
     $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     header("Location:$redirect");
  }



If you haven't even tried that then try it and you will be good to go.
If however that doesn't workas it didn't in my case, , and dumping $_SERVER variable doesn't give you HTTPS element in array either if its http or https then then you can have either of the two problems:

1. Your host is using some shared SSL certificate which uses the same port of 80 as for http and handling security itself, for that you would need to request your host to enable $_SERVER['HTTPS']

2. My case was even different. My client's website is on cloud hosting (something new at this point in time) and apparently it works somewhat different then others and has the array element $_SERVER["HTTP_X_FORWARDED_PROTO"] set to value "https" so the fix for me was:


if (!$_SERVER["HTTP_X_FORWARDED_PROTO"]=="https") {
    $newurl = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    header("Location: $newurl");
    exit();
}

Tuesday, April 3, 2012

How to make POST call in PHP using CURL

Ever wanted to make a POST or GET call to a URL without creating a form and submitting it? Well here is how you can do it using Curl, very simple.



<?php

// Submit those variables to the server
$postVals = urlencode("username")."=".urlencode('TomCruise');
$postVals.= "&";
$postVals.= urlencode("password")."=".urlencode('GhostProtocol');

// Send request to the required URL
$url = "http://www.b4blinky.com/index.php";

$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt( $curl, CURLOPT_POSTFIELDS,  $postVals);
curl_setopt( $curl, CURLOPT_POST, 1);
$result = curl_exec( $curl );
curl_close( $curl );

?>