Forum Moderators: coopster

Message Too Old, No Replies

PHP help.

         

zanatos

1:14 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Before I begin I should say I am very new to PHP. However I have done other programming like java,C/C++.

I a am making a small 2 page website.

My first page "product1.php" includes the following code:
---other head information are here--

<body>
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?php
echo "Today's date: ". date("G:i - m/d/y");

?>
<img src="images/Header.gif" />

</td>
</tr>
<tr>
<td><img src="images/Navbar.gif" /></td>
</tr>
<tr>
<td bgcolor="#000000"><h2 class="style2">Dell XPS</h2></td>
</tr>
</table>
</body>
</html>

Now I would like to see if the user has visited the site before, using cookies.

I am thinking of doing something like

if(isset($_COOKIE['lastVisit']))
$visit = $_COOKIE['lastVisit'];
if(isset($_COOKIE['VisitedPage']))
$visit = $_COOKIE['VisitedPage'];

echo "Your last visit was - ". $visit;

Now this is where my understanding of PHP lacks. Where would I put that code does it make sense to put it after the date? is my cookie code correct? If any one has any websites that can help me with this it would be greatly apreciated.

I would also like to output when the sale will end or show the number of the days until the sale ends. I assume I would do this with the DATE fuction but I am having trouble understanding how to do this. Again if anyone has any websites that will help me understand this better....please let me know!

Thank you!

zanatos

4:44 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Should Of been more clear on the date. I would like to do a countdown until the sale end and also can I change the text colour of the date output?

Thanks.

d40sithui

5:30 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Hello,
Using cookies for tracking users is fine. Just remember that cookie data can be manipulated by the user so don't store anything too sensitive. Also, and this is probably not a huge problem anymore, but cookies can be turned off by the browser settings.
I would also suggest you use the database to track users. This is a bit more involved since you will probably use their IP as the identifier and saving that in the database along with the times visited, etc.
In regards to the countdown, take a look at the code snippet below. It works well for me. You just need a little bit of Javascript.

<html>
<head>
<script language="JavaScript">
function StartCountDown(myDiv,myTargetDate){
var dthen = new Date(myTargetDate);
var dnow = new Date();
ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(myDiv,gsecs);
}

function Calcage(secs, num1, num2){
s = ((Math.floor(secs/num1))%num2).toString();
if (s.length < 2){
s = "0" + s;
}
return (s);
}
function CountBack(myDiv, secs){
var DisplayStr;
var DisplayFormat = "%%D%% Days %%H%%:%%M%%:%%S%%";
DisplayStr = DisplayFormat.replace(/%%D%%/g,Calcage(secs,86400,100000));
DisplayStr = DisplayStr.replace(/%%H%%/g, Calcage(secs,3600,24));
DisplayStr = DisplayStr.replace(/%%M%%/g, Calcage(secs,60,60));
DisplayStr = DisplayStr.replace(/%%S%%/g, Calcage(secs,1,60));

//if the time has not expired
if(secs > 0){
document.getElementById(myDiv).innerHTML = DisplayStr;
setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 1000);
}
//time has expired
else{
document.getElementById(myDiv).innerHTML = "Countdown has ended.";
}
}
</script>
</head>
<body>
<div id="clock1"></div>

<script language="JavaScript">
//initiates countdown
StartCountDown("clock1","06/18/2009 09:50 AM -0400");
</script>
</body>
</html>

So all you need to do is change the date in the line and you should be good to go.


StartCountDown("clock1","06/18/2009 09:50 AM -0400");