Jump to content

[SOLVED] shell_exec problem


Nandini

Recommended Posts

Hi i am doing change timezones using php

If user select one timezone as Hong_Kong. The system time was automatically change to current Hong_Kong time.

for this i am executing the following command by using shell_exec.

 

if ($q=="Asia/Hong_Kong")

{

putenv("TZ=Asia/Hong_Kong");

$time = date("g:i a");

echo $time;

$command=shell_exec('ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime');

echo "<pre>$command</pre>";

}

 

By using above script the system time was not changing. If i am using that linux commnd (ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime) in command line its working fine. But through script its not working.

 

Can anyone help me.

 

Link to comment
https://forums.phpfreaks.com/topic/126248-solved-shell_exec-problem/
Share on other sites

I don't know what you are trying to do with shell command

 

just adding the command

 

putenv("TZ=Asia/Hong_Kong"); 

 

at the top of the page will work fine.

May be you could save the part "TZ=Asia/Hong_Kong" in session if you need to assign different time zones for different user.

Hi here is the total code.

 

the following is my html form.

 

<script type="text/javascript" src="clienthint.js"></script>

<form>

Select timezone:

<select name="customers" onchange="showHint(this.value)">

<option value="America/Santiago">America/Santiago</option>

<option value="Asia/Hong_Kong">Asia/Hong_Kong</option>

<option value="Asia/Calcutta">Asia/Calcutta</option>

</select>

</form>

</td><td>

<div id="txtHint"><b> </b></div>

 

clienthint.js

 

// JavaScript Document

var xmlHttp

 

function showHint(str)

{

if (str.length==0)

  {

  document.getElementById("txtHint").innerHTML="";

  return;

  }

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  }

var url="get_time.php";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

}

 

function stateChanged()

{

if (xmlHttp.readyState==4)

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}function GetXmlHttpObject()

{

var xmlHttp=null;

try

  {

  // Firefox, Opera 8.0+, Safari

  xmlHttp=new XMLHttpRequest();

  }

catch (e)

  {

  // Internet Explorer

  try

    {

    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

    }

  catch (e)

    {

    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

    }

  }

return xmlHttp;

}

 

get_time.php

 

<?php

header("Cache-Control: no-cache, must-revalidate");

// Date in the past

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

 

//get the q parameter from URL

$q=$_GET["q"];

if ($q=="America/Santiago") { putenv("TZ=America/Santiago"); $time = date("g:i a"); echo $time; }

if ($q=="Asia/Hong_Kong") { putenv("TZ=Asia/Hong_Kong"); $time = date("g:i a"); echo $time;

$command="ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime"; ////this command is for change system time

echo shell_exec($command);

}

if ($q=="Asia/Calcutta") { putenv("TZ=Asia/Calcutta"); $time = date("g:i a"); echo $time;

$command="ln -sf /usr/share/zoneinfo/Asia/Calcutta /etc/localtime";

echo shell_exec($command);

}

?>

 

This won't work as you are setting the time zone in a different page.

 

For this to work you need to set it on the top of every page or in the configuration file which is included in every file.

 

Try to put the code

putenv("TZ=America/Santiago");

 

on top of your page and try to print the same.

 

the shell command you are using is of no use. It just builds a link to the file specified.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.