The above would get the host name not the ip address. If you wanted just the ip, you would have to use $_SERVER["REMOTE_ADDR"];
Printable View
So, to get back to the first question; to remember a language choice, I just place the following into the choosen language ? Correct ?
Code:$cookiename = "tracker";
$olddata = $_COOKIE[$cookiename];
$newdata = "Visited page " . $_SERVER['PHP_SELF'];
$data = $olddata . $newdata;
setcookie($cookiename,$data,time()+3600);
And does the above redirect at next visit ?
chechu, so sorry to interrupt again.
Thanks to tech_support and thetestingsite for your input.
May I know after creating the cookies, how to I test and view the result?
The above would save a cookie for the pages that the user has visited. To make the language one, simply change a few items.
As for joycie, you could use the following to check the data.Code:$cookiename = "lang";
$data = $languageChoice; //the variable for the language that was choosen.
setcookie($cookiename, $data, time()+3600);
Hope this helps both of you.Code:$theCookie = $_COOKIE['tracker']; //change "tracker" to your saved cookiename.
echo $theCookie; //echos the data stored in the cookie.
Sorry, but I'm really no good at php.
What goes on the selection page, and what goes on the selected page ? In the <head> ? Do I need to put <!? php somewhere as well ?
Questions from a dummie ...
Lets make a simple file (call it langTest.php), in this file we will place the following code:
That should do it, let me know if you need any more help.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
}
}
?>
Would appreciate if someone could help:
Tested using the above tracking php codes but returned with error--
Parse error: syntax error, unexpected T_STRING on line 7
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);
?>
<html>
<head>
<title>Test</title>
</head>
<body>hello
</body>
</html>
unless I am mistaken, and I might be, I never really knew much about php and I am forgetting more everyday...
is missing it's semicolon *;* at the end, and the parse error is coming from trying to combine the last two lines into one, as there is no separator.Quote:
$ip = $_SERVER["REMOTE_ADDR"]
Thanks to BLiZZaRD.
I use the codes below and when I go to the page http://domain.com/tracker.php, the page shows this message "Visited page /tracker.php"
Is this the correct way to call? Why is my IP and date visited not shown on the page? Pardon me for my query. I am a newbie in PHP. Thanks for any help.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);
?>
<?php
$theCookie = $_COOKIE['tracker'];
echo $theCookie; //echos the data stored in the cookie.
<html>
<head>
<title>Test</title>
</head>
<body></body>
</html>