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
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.

Link to comment
Share on other sites

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

}

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.