Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, August 31, 2012

Index PHP Array to use in Javascript / Javascript array

So here is what I wanted, I wanted to the text of check box when clicked on check box like
<input type='checkbox' value='1' name='chkbox'>First chk box</input>

So I wanted 'First chk box' when clicked on it, in here you might be able to simply get it using innerHTML but my case was bit different so I got a php array that had id=>value (1=>'First chk box') array now the issue is you can't use it in javascript

So step 2 is converting php array to javascript array, following are the things you can do

1. Simply make a javascript array and put php values in it
var jsArray = new Array('<?=$phpArr[0]?>',<?=$phpArr[1]?>',....);

That doesn't work in my case because my indexes were different

2. You can have insertion in JS Array using the splice function to insert in mid, you can google that, but the issue with that is you can't have a javascript array like

arr[1] = index1
arr[2] = index2
arr[5] = index5

means you need EACH index defined which is not my case, I could have skipping index so here is solution for me, or you if you have similar issue... or any of above might work for you...

3. var jsArray = <?php echo json_encode($phpArray) ?>;
Now if could do is simply using
jsArray[chkbox.value] 
and it would give me 'First chk box'

Friday, August 24, 2012

Generate Random Password Joomla and PHP

The following few lines creates a random password and puts it into and md5 encrypted format to be saved in joomla database. If you are using non-encrypted password you can just use the first part, or if you are using simple md5 encryption and not looking for key:salt as joomla then you can simply take the $key from first part and encrypt it using the md5 function and store that. :)

        $key = "";
        srand((double)microtime() * rand(1000, 9999));
        $charset = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
        for($len=0; $len<8; $len++)
            $key .= $charset[rand(0, strlen($charset)-1)];
        //End of part 1, you will have a simple random password here in $key
        $salt = JUserHelper::genRandomPassword(32);
        $crypt = JUserHelper::getCryptedPassword($key, $salt);
        $password = $crypt . ':' . $salt;

$password will have the value that you can save into database in users table.

Wednesday, August 1, 2012

Remove parameter from URL PHP

If you are looking to remove a specific parameter from the URL here is how to do it:

Assume you have URL
http://www.b4blinky.com/index.php?option=com_content&view=article&id=1
And you want to remove the id


$currURL = $_SERVER[QUERY_STRING];
//this will get you option=com_content&view=article&id=1
parse_str($currURL,$params); //Puts the url parameters in key=>value pair
//This will give you option=>com_content, view=>article, id=>1
unset($params[$fieldName]);  //Removes the parameter on which 'x' is clicked
//This will remove the $fieldName from the array e.g. 'id'
$newURL = http_build_query($params); //Rebuilds the query with the remaining parameters
//This will rebuild array excluding id
$newURL = 'index.php?'.$newURL;
//and finally your newURL like
index.php?option=com_content&view=article

That you can modify if relative doesn't work for you.

Hope it helps. Happy coding :)

Sunday, July 22, 2012

Technorati API Closed

Technorati was one of the most popular used website few years back but as it started to decline sadly the team also seemed not to care much about it. They closed their API that was widely used by people and developers worldwide in 2009 promising for something new but its been three years and there is nothing which leaves a heavy doubt that probably it won't be coming ever again.

Here is the link where the announced the closure of existing API and announcement of new API and features which never came.
http://technorati.com/developers/

Here is an interesting blog article by another person who mentioned that even the founder of Technorati used the API on his blog and I checked it now and there was nothing of Technorati.
http://blog.programmableweb.com/2010/03/04/technorati-api-disappears-no-longer-representing-the-technorati/

Tuesday, May 1, 2012

Joomla redirect from view.html.php

If you ever want to redirect url from view of a component i.e. view.html.php simply use the following two lines:

$app =& JFactory::getApplication(); 
$app->redirect('index.php?option=com_mycomponent&view=default'); 

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

?>