-
You would have to do something like the following in order to get it to display right:
Code:
<?php
$cookiename = "tracker";
$olddata = $_COOKIE[$cookiename];
$newdata = "Visited page " . $_SERVER['PHP_SELF'];
$data = $olddata . $newdata;
$ip = $_SERVER["REMOTE_ADDR"];
setcookie($cookiename,$data,time()+3600);
$theCookie = $_COOKIE['tracker'];
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
echo $theCookie; //echos the data stored in the cookie.
echo 'IP Address: '.$ip; //echos IP Address found by REMOTE_ADDR above
?>
</body>
</html>
Hope this helps.
-
Aye, looks good to me, and just to explain incase you couldn't tell by test's script there, in your original code you were calling the variable $theCookie;
Yet in your script definitions "theCookie" was never defined. So basically you were giving your scripts names for all it's apples then asking it to show you an orange.
Just notice how test's script defines what $theCookie actually is, so when it is called for in the echo' the script KNOWS what to display :)
-
Thanks for explaining it for me Blizzard, I was looking at it last night and thought it could use some explaining, but never got around to it.
-
No problem buddy, you do it for me as well. Team work and all that. Plus later when I have the same problems cause I am so forgetful, I can look at my own posts and answer myself...
ROTF!
-
Many thanks to both thetestingsite and BLiZZaRD for your time and patience.
I could now view my own IP when I access the php file online.
If I want to keep a record of what people do when they visit a webpage of my site, say downloading picture or songs, etc, may I know how I could modify the script so that only I (and not others) could view the IPs of people who visit my site and what he/she did on my site?
Any help would be greatly appreciated.
-
That is a tricky question.
Yes there are ways to make it so you can find and follow an IP around your site. I am not so sure you will be able to "see" what they are doing.
About the best you could do is see which pages they go to and for how long.
One of the best (and easier) ways to do this is to get a high profile stat counter.
look into BBClone. Simple set up, updated often and has more stats than you can remember. I used it very well for a very long time until my site grew so large and popular that it was starting to fall behind.
-
I this code has something wrong in it. I am looking for something that gives the user a drop down box they pick the langage that they want. after clicking go it takes them to http://www.YOURDOMAIN.com/en or http://www.YOURDOMAIN.com/ch
Code:
<?php
$homepage = 'http://'.$_SERVER["HTTP_HOST"].'/';
if ($_REQUEST['act'] == "setLang") {
//check to see if the form has been submitted!
if ($_REQUEST['lang'] == "") {
header('Location: '.$_SERVER["PHP_SELF"]);
//if language selection is empty, redirect to form!
}
else {
//if language was selected, save it in a cookie, then redirect to appropriate page!
$lang = $_REQUEST['lang'];
setcookie("language, $lang, time()+3600);
header('Location: '.$homepage.$lang);
}
}
else {
//if form has not been submitted
if (@$_COOKIE['language'] != "") {
/* check to see if language cookie is empty. If not, redirect to appropriate page. */
header('Location: '.$homepage.$_COOKIE["language"]);
}
else {
//if cookie is empty, display form
?>
<html>
<head>
<title>Language Cookie Test</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="act" value="setLang">
<select name="lang">
<option value="en">English</option>
<option value="sp">Spanish</option>
<option value="ru">Russian</option>
</select>
<input type="submit" value="Set Language">
</form>
</body>
</html>
<?php
}
}
?>
Also i need it to remember what the user choosed.
-
You say there is something wrong with the code, but you don't say what it is/isn't doing. By looking at the code, it should work fine; however, if you say it has a problem, let us know what it is.
-
Hey,
Below you will find the cookie to remember the language choice.
But this one is using a form. How is the code, if I let people choose by just clicking a test link ? (see here ?
Code:
<?php
$homepage = 'http://'.$_SERVER["HTTP_HOST"].'/';
if ($_REQUEST['act'] == "setLang") {
//check to see if the form has been submitted!
if ($_REQUEST['lang'] == "") {
header('Location: '.$_SERVER["PHP_SELF"]);
//if language selection is empty, redirect to form!
}
else {
//if language was selected, save it in a cookie, then redirect to appropriate page!
$lang = $_REQUEST['lang'];
setcookie("language, $lang, time()+3600);
header('Location: '.$homepage.$lang);
}
}
else {
//if form has not been submitted
if (@$_COOKIE['language'] != "") {
/* check to see if language cookie is empty. If not, redirect to appropriate page. */
header('Location: '.$homepage.$_COOKIE["language"]);
}
else {
//if cookie is empty, display form
?>
<html>
<head>
<title>Language Cookie Test</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="act" value="setLang">
<select name="lang">
<option value="en">English</option>
<option value="sp">Spanish</option>
<option value="ru">Russian</option>
</select>
<input type="submit" value="Set Language">
</form>
</body>
</html>
<?php
}
}
?>
-
To save the cookie as a link, try the following.
Code:
<?php
$homepage = 'http://'.$_SERVER["HTTP_HOST"].'/';
if ($_REQUEST['act'] == "setLang") {
//check to see if the form has been submitted!
if ($_REQUEST['lang'] == "") {
header('Location: '.$_SERVER["PHP_SELF"]);
//if language selection is empty, redirect to form!
}
else {
//if language was selected, save it in a cookie, then redirect to appropriate page!
$lang = $_REQUEST['lang'];
setcookie("language, $lang, time()+3600);
header('Location: '.$homepage.$lang);
}
}
else {
//if form has not been submitted
if (@$_COOKIE['language'] != "") {
/* check to see if language cookie is empty. If not, redirect to appropriate page. */
header('Location: '.$homepage.$_COOKIE["language"]);
}
else {
//if cookie is empty, display form
?>
<html>
<head>
<title>Language Cookie Test</title>
</head>
<body>
<a href="?act=setLang&lang=en">English</a>
<a href="?act=setLang&lang=sp">Spanish</a>
<a href="?act=setLang&lang=ru">Russian</a>
</body>
</html>
<?php
}
}
?>
Hope this helps.