edwinhermann 57 Junior Poster

Khan - please start a new thread. This thread is solved and it's not appropriate to post additional messages here.

edwinhermann 57 Junior Poster

My approach would be to create not so much as a database but just a flat file, say tab delimited. You can read the entire thing into memory with PHP and manipulate the data as needed.

You could either run a web server locally (Macs have this already built in) or run it command-line based if suitable.

You'd want it to prompt for a username/password before proceeding.

You would write functions to add a new row of data, delete a row given an id, change a value given an id and column name, etc.

The entire project could be encrypted with PHPshadow. That's the cheapest PHP encryption I've found - only 5 Euros to encrypt (during a 48-hour period) or 0.99 if you do it online. Only thing is it is not available for Windows.

Regarding javascript encryption, remember that if the browser can see it, so can you. In other words, javascript encryption is pointless.

edwinhermann 57 Junior Poster

The dots get changed to underscores. So use this instead:

$linex = $_GET['shapes_image_x'];
$liney = $_GET['shapes_image_y'];
edwinhermann 57 Junior Poster

The Content-Type header must be sent before the Content-Disposition header.

edwinhermann 57 Junior Poster

Neither of these are the correct approach. strtotime turns the date into an integer. You then subtract one integer from the other which gives you a difference. You are then trying to turn that difference into a date (which will result in a date close to the epoch depending in the size of the difference).

You should be using DateTime::diff - see http://php.net/manual/en/datetime.diff.php There are worked examples there.

diafol commented: good +15
edwinhermann 57 Junior Poster

Are the other scripts also PHP? If so, you can call them using veal.

Like this:

<?php
// Do some stuff
eval(file_get_contents('?>'.'another_php_scrpt.php'));
// Do other stuff  <-- this will only start when the other php script has finished executing
?>

Then when you call this main script from CLI, the other php script will also be executed in due course but in the foreground.

edwinhermann 57 Junior Poster

Of course. Usually it's in /usr/bin/php

Use this syntax:

/usr/bin/php[I] filename.php[/I]
edwinhermann 57 Junior Poster

Use the $_SESSION array instead. So, for example:

$_SESSION["name"]="foobar";

So now you should not use session_register(), session_unregister() or session_is_registered().

Can you can test using:

if (isset($_SESSION["name"])) { ... }
edwinhermann 57 Junior Poster

http://www.ioncube.com/

They also have a "pay-as-you-go" option where you can upload the code and compile it for much less than you would pay to buy the whole package.

Any protection can be broken but the "byte-code" approach used by Ioncube and Zend is the most secure. Encryption (like PHPShadow) can be easily broken. There are solutions posted on Forums like this that demonstrate how to do it.

PHPshadow is not easily broken. It uses a proprietary encryption algorithm. Sure, it's not impossible to break, but it's certainly extremely difficult. Most importantly, it has not been cracked yet (ionCube and Zend have been).

Secondly, byte code can be decompiled. Decompiling does not give you back the exact same source code, but it does produce a version of code that does effectively the same thing as the original.

edwinhermann 57 Junior Poster

PHPShadow: http://www.phpshadow.com

This is probably the most cost effective solution because you only need to buy a licence (5 euro) when you encrypt your source code. Once encrypted, your source code will work forever and you don't have to pay them another penny.

edwinhermann 57 Junior Poster

Do you actually have the characters <name> in your file? Otherwise you'll need to specify whatever it is that is in the file that you want replaced.

edwinhermann 57 Junior Poster

Then why not simplify like this:

function getdata()	{
return file_get_contents("titel.txt");
}
$content = getdata();
$newvalue = "Fred";
$new_text = str_replace("<name>",$newvalue,$content);
echo $new_text;

Or even better do away with the function, so like this:

$content = file_get_contents("titel.txt");
$newvalue = "Fred";
$new_text = str_replace("<name>",$newvalue,$content);
echo $new_text;
diafol commented: welcome back +14
edwinhermann 57 Junior Poster

This is a new question and you should really have started a new thread. But never mind, we're here now :)

Here's some code:

<?php
$text = "It happened when <name> turned his computer on.";
$newvalue = "Fred";
$new_text = str_replace("<name>",$newvalue,$text);
echo $new_text; // It happened when Fred turned his computer on.
?>
edwinhermann 57 Junior Poster

You're only opening the file. You're not outputting it.

Try changing

$file=fopen("welcome.txt","r");

to

readfile("welcome.txt");
edwinhermann 57 Junior Poster

I presume you're using PuTTY to initiate an SSH connection.

If so, try the editors nano or pico

e.g. nano index.php

edwinhermann 57 Junior Poster

Are you sure you have the sockets module installed in your PHP installation?

Check by looking at the output of phpinfo()

edwinhermann 57 Junior Poster

PHPshadow (http://www.phpshadow.com) encrypts PHP code, and requires a license code (a file) to be copied onto the server before it will work.

One option is a 50 Euro one-off payment per customer (it's locked to a specific domain name).
The other option is an annual payment of 20 Euro.

Because you are providing software to your clients, it's probably the one-off payment option you want (unless you can be bothered arranging renewals each year).

If you're likely to sell lots of copies of your software, you can always approach them about a bulk license arrangement.

edwinhermann 57 Junior Poster

Most routers mask the internal IP address.

edwinhermann 57 Junior Poster
echo substr($search,0,4); //this would print out "Hello"

Not quite. That would print out "Hell" (4 characters)

decade commented: thank you +1
edwinhermann 57 Junior Poster

Thank you, but that's not my question at all.

Well the answer I provided is the same as the answer to your question. Perhaps I assumed you'd make the link. So, to be explicit:

I mean where did the 'key' and 'value' come from although they're not defined in the file at all??

'key' and 'value' are keys of the array $x which are created as part of the each() function.

edwinhermann 57 Junior Poster

I agree with evstevemd.

foreach ($array as $key => $value) {
  echo 'The key is '.$key.' and the value is '.$value; 
 }

$key and $value are created as part of the foreach statement.

edwinhermann 57 Junior Poster

You can use cron to call a php page.
IN your crontab file, the command part would be something like this:

/usr/bin/php /users/me/www/myfile.php
edwinhermann 57 Junior Poster

You came here for a suggestion and got a complete solution. Lucky guy.

I just re-read the original post, and now I realise it was for an assignment. I suppose I shouldn't have provided the complete solution. Oh well :)

edwinhermann 57 Junior Poster

ok but, in case when we have an entire text file containing thousands of words, what approach can we take ?

See here: http://www.daniweb.com/web-development/php/threads/369522

edwinhermann 57 Junior Poster
<?php
$text = "one\ntwo\nthree\nfour";
echo preg_replace("/\\n/m"," \n",$text);
?>
edwinhermann 57 Junior Poster

I think this would work reasonably fast:

<?php
$filename = "/path/to/file.txt";
$handle = fopen($filename,"r");
if ($handle === false) {
  exit;
  }
$word = "";
while (false !== ($letter = fgetc($handle))) {
  if ($letter == ' ') {
    $results[$word]++;
    $word = "";
    }
  else {
    $word .= $letter;
    }
}
fclose($handle);
print_r($results);
?>

Note: This assumes the file is in the format <word><space><word><space><word>.... etc.

edwinhermann 57 Junior Poster

This page does the calculation. It uses DateInterval so requires PHP 5.3.0 or later.

<?php

$age = $_GET["age"];
$year = $_GET["year"];
$month = $_GET["month"];
$day = $_GET["day"];

if (!$_GET["age"]) {
  }
elseif (!checkdate($month,$day,$year)) {
  echo "Invalid Date";
  }
else {
  $one_year = new DateInterval("P1Y");
  $one_day = new DateInterval("P1D");
  $age_years = new DateInterval("P".$age."Y");
  $min_date = new DateTime(sprintf("%04d",$year)."-".sprintf("%02d",$month)."-".sprintf("%02d",$day),new DateTimeZone("Pacific/Auckland"));
  $min_date -> sub($age_years);
  $min_date -> sub($one_year);
  if ( ($min_date -> format("j") == $day) && ($min_date -> format("n") == $month) ) {
    $min_date -> add($one_day);
    }
  $max_date = new DateTime($year."-".$month."-".$day,new DateTimeZone("Pacific/Auckland"));
  $max_date -> sub($age_years);
  if ( ($max_date -> format("j") != $day) || ($max_date -> format("n") != $month) ) {
    $max_date -> sub($one_day);
    }
  echo '<p>If the person was <b>'.$age.'</b> on <b>'.sprintf("%04d",$year).'-'.sprintf("%02d",$month).'-'.sprintf("%02d",$day).'</b>, they must have been born between <b>'.$min_date->format("Y-m-d").'</b> and <b>'.$max_date->format("Y-m-d").'</b>.</p>';
  }
$months = array(1=>'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
echo '<hr>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">';
echo '<p>Age: <input type="text" name="age" maxlength="3" style="width: 30px;"></p>';
echo '<p>Year: <input type="text" name="year" maxlength="4" style="width: 40px;"> Month: <select name="month"><option selected value="0"></option>';
for ($i=1;$i<=12;$i++) {
  echo '<option value="'.$i.'">'.$months[$i].'</option>';
  }
echo '</select> Day: <select name="day"><option selected value="0"></option>';
for ($i=1;$i<=31;$i++) {
  echo '<option value="'.$i.'">'.sprintf("%02d",$i).'</option>';
  }
echo '</select></p>';
echo '<p><input type="submit" value="Calculate"></p>';
echo '</form>';

?>

And if you don't have PHP 5.3.0, you can achieve the same thing with this code:

<?php

$age = $_GET["age"];
$year = $_GET["year"];
$month = $_GET["month"];
$day = $_GET["day"];

if (!$_GET["age"]) {
  }
elseif (!checkdate($month,$day,$year)) {
  echo "Invalid Date";
  }
else {
  $min_date_year = $year - $age - 1;
  $min_date = (checkdate($month,$day,$min_date_year)) ? mktime(0,0,0,$month,$day,$min_date_year)+86400 : mktime(0,0,0,3,1,$min_date_year);
  $max_date_year = $year - $age;
  $max_date = …
edwinhermann 57 Junior Poster

Here I'm assuming you will be executing the code as the user logs in. So you can do this:

$random_hour = randbetween(0,23);
$current_hour = date("G",time());
if ($current_hour == $random_hour) {
  // something great happens
  }

If you're trying to implement it on a weekly cycle (i.e. choosing a random hour on a random day) then use this code when the user logs in:

$random_hour = randbetween(0,23);
$random_day = randbetween(1,7);
$current_hour = date("G",time());
$current_day = date("N",time());
if ( ($current_hour == $random_hour) && ($current_day == $random_day) ) {
  // something great happens
  }
edwinhermann 57 Junior Poster

It's easier to troubleshoot if you arrange your code neatly, like this:

<?php
if (empty($mobile)) {
  echo '<script language="javascript">alert("Enter Phone Number")</script>;';
  }
elseif (!(empty($mobile))) {
  if ((is_numeric($mobile))) {
    if ((strlen($mobile)!=10))
      echo '<script language="javascript">alert("phone number should be of 10 digit")</script>;';
    }
  else {
    echo '<script language="javascript">alert("Please Enter valid phone number")</script>;';
    }
  }
elseif (!($_FILES["uploadedfile"]["type"]=="image/pjpeg" ||  $_FILES["uploadedfile"]["type"]=="image/jpeg" ||  $_FILES["uploadedfile"]["type"]=="image/jpg" ||  $_FILES["uploadedfile"]["type"]=="image/png")) { 
  echo "i am in upload";		    
  echo "<script language=\"javascript\">alert(\"Your uploaded file must be of JPG or GIF. Other file types are not allowed!!\")</script>;";  
  echo "<script type='text/javascript'>";
  echo "</script>";
  }
else {
  mysql_connect('localhost','root','');
  mysql_select_db('project');
  mysql_query("SET NAMES utf8");
  //$result=mysql_query("Select * from tbl_hindi");
  echo("INSERT INTO `project`.`tbl_hindi` (`pre`, `name`, `father`, `state`, `city`, `gender`, `mobile`, `dob`, `namdan_date`, `file` ,`relation`)VALUES ('$pre','$name','$father','$state','$city','$gender','$mobile','$dob','$namdan_date','$file','$relation')");
  }
?>

That makes it easier to work out what is going wrong. If you examine your logic closely you'll see that it will never get to the Upload section (or the mysql section). Why? Because you code is effectively saying (in pseudo-code):

if (is empty) {
  // do stuff
  }
elseif (is not empty) {
  // do other stuff
  }
elseif (some other condition) {
  // do upload stuff
  }
else {
  // do mysql stuff
  }

The variable is either empty or not empty, so that's why it never gets to upload stuff. It's like saying "If it's raining then put on your raincoat, if it's not raining then put your raincoat away, otherwise go shopping". You never get to go shopping because you only end up follow the first two branches because it's …

edwinhermann 57 Junior Poster

One way is to have a transparent DIV that takes up the whole screen with a high z-index value, but still lower than the sign-in DIV. On that DIV you'd have something like this:

<div onClick="cancelSignIn()">

And the corresponding Javascript:

function cancelSignIn() {
document.getElementById("signin").name=0;
document.getElementById("twitter").style.display='none';
}

A full-page DIV an be achieved by putting it after the <body> tag and applying the following CSS (assuming the DIV's id is "canceltwitter":

div#canceltwitter {
position: absolute;
margin: 0px;
padding: 0px;
border: 0px;
width: 100%;
height: 100%;
z-index: 50;
display: none;
}

You'd need to also set the z-index of the Twitter div to something greater than 50, and use javascript to set the "display" property of the canceltwitter div to block at the same time as displaying the twitter div. (Also putting the canceltwitter div's display property back to none as part of the cancelSignIn() function).

edwinhermann 57 Junior Poster

Just elaborating on twiss's post:

<script type="text/javascript">
function doSomething(elem) {
alert ('The ID of the element which triggered this is: ' + elem.id;
}
</script>
<div id="blah" onClick="doSomething(this)">Blah blah blah</div>
edwinhermann 57 Junior Poster

Hahaha yep ardav! Doh! :)

edwinhermann 57 Junior Poster

Look at line 21 of your code:

echo '</div>'

You are missing the semi-colon at the end of the line, i.e. like this:

echo '</div>;'
edwinhermann 57 Junior Poster

look at line 15 in my code , it is there already, does not work though

<td><input name="Submit" type="image" value="Upload image"  src="images/button_sub2.png"></td>

That is indeed odd.
Can you browse to http://yourwebsite/images/button_sub2.png and see the image that way? Also try removing value="Upload image"

edwinhermann 57 Junior Poster

You want this:

<input type="image" src="blah.png">
edwinhermann 57 Junior Poster

You are welcome. Please don't forget to mark this thread "Solved" when ready.

edwinhermann 57 Junior Poster

Yes, you can for the name attribute but no not for the id attribute.
But in any event, the name attribute is never used because the form is never submitted; from what I can tell you are simply using input boxes to display information. The only reason to keep the name attribute is to produce HTML that is W3C compliant.

edwinhermann 57 Junior Poster

You cannot have multiple tags with the same id. Your code will output multiple instances of <input id="rate"> and <input id="qty">.

You should serialise them, like this:

<?php
	include("config.php");
?>
<script type=text/javascript>
function multiply(id){
var a;
var b;
var c;
a = document.getElementById("rate" + id).value;
b = document.getElementById("qty" + id).value;
document.getElementById("amt" + id).value = a * b;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<form action="" method="get" name="test">
<table width="800" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">S.No</td>
    <td align="center">Item Name</td>
    <td align="center">Item Code</td>
    <td height="30" align="center">Rate</td>
    <td align="center">QTY</td>
    <td align="center">Amount</td>
    </tr>
  <tr>
  <?php $desr="select * from site_rate where category_code='EWS001' AND sitename='Amber' order by item_code";
		//echo $desr;
	  	$r=mysql_query($desr,$conn);
	  	$i=1;
		while($m=mysql_fetch_array($r))
		{
			?>
    <td><?php echo $i; ?></td>
    <td><textarea name="cat_item" cols="25" rows="2"><?php echo $m['category_item'];?></textarea></td>
    <td><?php echo $m['item_code'];?></td>
    <td><input name="rate<?php echo $i; ?>" type="text" id="rate<?php echo $i; ?>"/></td>
    <td><input name="qty<?php echo $i; ?>" type="text" id="qty<?php echo $i; ?>" onchange="multiply(<?php echo $i; ?>)"/></td>
    <td><input name="amt<?php echo $i; ?>" type="text" id="amt<?php echo $i; ?>" /></td>
    </tr><?php $i++; } ?>
</table>

</form>

</body>
</html>
edwinhermann 57 Junior Poster

AlmostBob's URL to the PHP site is fine, but his description is a bit off.
Slashes and spaces are fine. It's nothing to do with the character set but in fact to do with characters reserved for HTML code.

Imagine, for example that PHP_SELF contained a greater-than symbol like this: >

Then the rendered code without htmlspecialchars would be something like this:

<form action="aaaaa>bbbbb">

And that would render the HTML invalid because the end of the form tag is now before the bbbb's.

htmlspecialchars changes a few HTML-reserved characters into coded alternatives. The ones affected are:
ampersand
double quote
single quote
greater-than symbol
less-than symbol

So it's nothing to do with characters being lost in their journey between the browser and the web server, but more to do about generating valid HTML so that the browser understands it.

edwinhermann 57 Junior Poster

Thanks!

edwinhermann, I get this error with your code:
"Fatal error: Class 'DateInterval' not found "

You're probably on an older version of PHP. You'll need 5.3+ for my code.

You may want to try other code offered by other people, though they use "date()" which is best to avoid, so when you upgrade to PHP5.3 you may want to transition your code back to something like what I posted, using the DateTime andDateInterval classes.

edwinhermann 57 Junior Poster

This code does what you've asked, and starts looking from "tomorrow".

<?php
$dt = new DateTime("tomorrow",new DateTimeZone("Pacific/Auckland"));
$di = new DateInterval("P1D");
while ( ($dt -> format("l") != 'Tuesday') && ($dt -> format("l") != 'Saturday') ) {
  $dt -> add($di);
  }
echo 'Next sale: '.$dt->format("l, j F Y");
?>

Just replace the timezone with that of your locale, and of course you can change the format of the output as well.

edwinhermann 57 Junior Poster

You might have another 12 months before you need to sort it out. I just spotted this story:
http://www.bbc.co.uk/news/technology-13541250

edwinhermann 57 Junior Poster

I'm not in Europe so I don't know the law, but from the article it sounded more like the issue was around third party cookies only, and that cookies that only get transmitted to the issuing site for the purposes of allowing the site to function, is allowed. But I could be wrong.

edwinhermann 57 Junior Poster

Is your site one that people have to "sign in" to? If so, why not just build the consent into the standard terms and conditions that one must agree to in order to create an account?
If it's a public site and does not require users to sign in, it's a little more difficult. You could remember the consent setting in a cookie :-)

edwinhermann 57 Junior Poster

If I can just chime in here....

Might just want to add a space after Location:

edwinhermann 57 Junior Poster

You can still use ErrorDocument in .htaccess to invoke a customised page for Error 403, and then in the custom Error 403 page you put at the top:

<?php
header("HTTP/1.0 404 Not Found",true,404);
?>

That will send the correct HTTP 404 response.

edwinhermann 57 Junior Poster

I suspect it's that the setting "short_open_tag" is forced on, and so the first two characters of line 1 (the less-than symbol followed by the question mark) is causing PHP to start interpreting what is after that, which happens to be the contents of the XML header tag and not PHP code.

You can always verify whether short_open_tag is the problem by looking at phpinfo(). Simply create a PHP file with only the following content:

<?php phpinfo(); ?>

If php_open_tag is set to "On" then that is your problem.

The only way to disable this is to use .htaccess (provided your hosting company allows you to). You would need to add the following line into the .htaccess file:

short_open_tag on

If you are not sure whether .htaccess directives are allowed for you, you can always try it, and then check again using phpinfo() as above.

edwinhermann 57 Junior Poster

Here's how you should be using the Date/Time features in PHP to achieve the same thing:

<?php
$tz = new DateTimeZone('Europe/Paris');
$time = new DateTime("@1303889758",$tz);
$time -> setTimeZone($tz);
$convert = $time -> format("G:i:s d. n. Y. ");
//convert $time to our date: output is 9:35:58 27. 4. 2011. 
echo $convert."<br>";
$con2 = $time -> format("U");
//convert converted time to unix: doesn't work
echo $con2;
?>
edwinhermann 57 Junior Poster

Assuming your allow_url_fopen setting is enabled (see http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) then here's the code:

<?php
file_put_contents("/path/to/filename.html",file_get_contents("http://www.example.com/"));
?>
edwinhermann 57 Junior Poster

Yes, there's always a risk that someone breaks into the server and steals your code. Not only do they now have your IP, but also potentially passwords (to databases) etc.

You can encrypt your PHP code so that it's unreadable.

Try http://www.phpshadow.com