Add the following code in function.php to enable or start session in wordpress
add_action('init', 'activate_session', 1); function activate_session() { if(!session_id()) { session_start(); } }
First step you need to create a google client id
Go to https://console.developers.google.com/project/_/apiui/apis/library
then create a new project or select an existing project
Step.1
Step.2
I am going to create a new project (If you are having a project already you can select that one also)
Step.3
Enable google+ Api
Step.4
Choose an Email Address, specify a Product Name, and press Save.
In the Credentials tab, select the New credentials drop-down list, and choose OAuth client ID.
Under Application type, select Web application.
Copy the client id from the google developer console and past it in to the meta tag of the bellow index,html “<YOUR CLIENT ID>”
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <meta name="google-signin-client_id" content="<YOUR CLIENT ID>"></meta> <title>Google login </title> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> </head> <body> <div id ="signin" class="g-signin2" data-onsuccess="onSignIn"></div> <br>Access token<br> <textarea id="token" rows="" cols=""></textarea> <br> <button id="verify_in_server">verify in server</button> <div id="verify_in_server_result"></div> <a id ="signout" href="#" onclick="signOut();">Sign out</a> <script src="https://apis.google.com/js/platform.js" async defer></script> <script type="text/javascript"> /* * Google sign in callback * after loginin to google this function will automatically call * no neet call this function manually */ function onSignIn(googleUser) { var profile = googleUser.getBasicProfile(); var id_token = googleUser.getAuthResponse().id_token;// this token we will send to server to verify console.log('ID: ' + profile.getId()); console.log('Name: ' + profile.getName()); console.log('Image URL: ' + profile.getImageUrl()); console.log('Email: ' + profile.getEmail()); console.log('Token: ' + id_token); $("#token").text(id_token); } function signOut() { var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function() { console.log('User signed out.'); }); } $("#verify_in_server").click(function(){ $.ajax({ type: "POST", url: "verify.php", data: { token: $("#token").text()}, success: function(result){ $("#verify_in_server_result").html(result); } }); }); </script> </body> </html>
Server side verification (PHP)
<?php echo "Result from server<br>"; if (isset ( $_REQUEST['token'] ) && $_REQUEST['token'] != '') { $response = file_get_contents ( 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' . $_REQUEST['token'] ); $response = json_decode ( $response ); echo "<pre>"; print_r ( $response ); echo "</pre>"; } ?>
Out Put
ref
In this snippet I am just creating an useful function to replace string in between two string (including the given string )
<?php /** * function for replaceing sting between two string (included these two strings) * @param String $str_start <p>start string of search</p> * @param String $str_end <p>ending string of search</p> * @param String $replace <p>replace with</p> * @param String $string <p>source string (the original string we want to perform search)</p> * @return String */ function replace_between($str_start, $str_end, $replace, $source_string) { $startPos = strpos($source_string, $str_start); $endPos = strpos($source_string, $str_end); if ($startPos === false || $endPos === false) { return $source_string; } $textToDelete = substr($source_string, $startPos, ($endPos + strlen($str_end)) - $startPos); return str_replace($textToDelete, $replace, $source_string); } ?>
sample function calls
sample 1
$mystring = '<div>a b c d e f g a x c </div>'; $new_str = replace_between('a', 'c', 'W', $mystring); echo $new_str;
Output
<div>W d e f g a x c </div>
sample 2
$mystring = '<div>some text <a href="http://workassis.com">click me</a> </div>'; $new_str = replace_between('<a', '</a>', '', $mystring); echo $new_str;
Output
<div>some text </div>
$mystring = '<div>abc def gaxc hiks dfess ddfsdfed </div>'; $new_str = preg_replace('/def[\s\S]+?dfess/', '', $mystring); echo $new_str;
Output
<div>abc ddfsdfed </div>
ref: http://php.net/manual/en/function.strpos.php,
http://php.net/manual/en/function.substr.php,
<?php $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"password: mypassword\r\n" . "Accept-language: en\r\n" ) ); $context = stream_context_create($opts); $response = file_get_contents('my/api/url', false, $context); $results = json_decode($response); echo "<pre>"; print_r($results); echo "</pre>"; ?>
ref: – php.net
Download phpexcel : https://phpexcel.codeplex.com/
Download tcpdf : https://sourceforge.net/projects/tcpdf/files/
<?php //data to make csv (may be from database, form, etc) $dbdata = array( array("name"=>"name1", "phone"=>"1010101001"), array("name"=>"name2", "phone"=>"1010101002") ); require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php'; require_once 'PHPExcel/Classes/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); $rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF; // tcpdf folder $rendererLibraryPath = dirname(__FILE__).'/tcpdf'; //setting column heading $objPHPExcel->getActiveSheet(0)->setCellValue('A1',"Name"); $objPHPExcel->getActiveSheet(0)->setCellValue('B1',"Phone"); //setting column body $i=2; //starting from row 2 bcz row 1 set to header foreach($dbdata as $data) { $objPHPExcel->getActiveSheet(0)->setCellValue('A'.$i,$data['name']); $objPHPExcel->getActiveSheet(0)->setCellValue('B'.$i,$data['phone']); $i++; } // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); if (!PHPExcel_Settings::setPdfRenderer( $rendererName, $rendererLibraryPath )) { die( 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . '<br />' . 'at the top of this script as appropriate for your directory structure' ); } // Redirect output to a client’s web browser (PDF) header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="01simple.pdf"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); $objWriter->save('php://output'); exit; ?>
Download phpexcel : https://phpexcel.codeplex.com/
<?php //data to make csv (may be from database, form, etc) $dbdata = array( array("name"=>"name1", "phone"=>"1010101001"), array("name"=>"name2", "phone"=>"1010101002") ); require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php'; require_once 'PHPExcel/Classes/PHPExcel.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); //setting column heading $objPHPExcel->getActiveSheet()->setCellValue('A1',"Name"); $objPHPExcel->getActiveSheet()->setCellValue('B1',"Phone"); //setting column body $i=2; //starting from row 2 bcz row 1 set to header foreach($dbdata as $data) { $objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$data['name']); $objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$data['phone']); $i++; } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="export.xls"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); /* // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 */ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; ?>