By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

cookie

P: n/a
What's the best I can do to solve my trouble :

I got a simple page index-tmp who include a code snippet who test if a cookie
exist and if yes redirect to the user account page but the trouble
is when I didn't 've a cookie the page still redirect to the user account page.

here is the following code :

<?
$cookie = $_COOKIE["free-nickname"];
if (!cookie == null) {
echo '<script langage = "javascript">';
echo " document.location.href = 'index_utilisateur.php';";
echo '</script>;';
}
else {
echo '<script langage = "javascript">';
echo " document.location.href = 'index.php';";
echo '</script>;';
}
?>

thx in advance
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Your line :
if (!cookie == null) {

should read:
if (!$cookie == null)
(You missed the $ before the cookie variable name)

Thanks!

Jul 17 '05 #2

P: n/a
Damn I'm stupid thx Kartic :)

"Kartic" <ka******************@gmail.com> wrote:
Your line :
if (!cookie == null) {

should read:
if (!$cookie == null)
(You missed the $ before the cookie variable name)

Thanks!

Jul 17 '05 #3

P: n/a
Alexandre wrote:
What's the best I can do to solve my trouble :

I got a simple page index-tmp who include a code snippet who test if a cookie
exist and if yes redirect to the user account page but the trouble
is when I didn't 've a cookie the page still redirect to the user account
page.

here is the following code :

<?
$cookie = $_COOKIE["free-nickname"];
if (!cookie == null) {
echo '<script langage = "javascript">';
echo " document.location.href = 'index_utilisateur.php';";
echo '</script>;';
}
else {
echo '<script langage = "javascript">';
echo " document.location.href = 'index.php';";
echo '</script>;';
}
?>

thx in advance


I think "if ($cookie != null)" might work better.

Don't use Javascript to redirect visitors -- HTTP redirects are much safer.
I suggest you make index.php your default home page and put this script at
the top of the file:

<?php

$cookie = $_COOKIE["free-nickname"];
if ($cookie != null) {
header("HTTP/1.1 302 Found");
header("Location: http://www.yourdomain.com/index_utilisateur.php");
exit;
}
else {
header("Vary: Cookie");
}

....
?>

P.S. To include javascript in an HTML file, use <SCRIPT
type="text/javascript">, not <SCRIPT lang(u)age="javascript">

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Jul 17 '05 #4

P: n/a
.oO(Alexandre)
I got a simple page index-tmp who include a code snippet who test if a cookie
exist and if yes redirect to the user account page [...]


Why are you using unreliable client-side redirection? Do it properly and
use a Location header with an absolute URL:

header('Location: http://www.example.com/foo');

Micha
Jul 17 '05 #5

P: n/a
With the Location header you can make sure that all your visitors will
be redirected to the page you want to redirect them to. Think of
lynx/links users who do not use a javascript enabled browser. Also,
please note that the header() must go before any output is made on the
page.

Good luck
Clau

Jul 17 '05 #6

P: n/a
great thx =)

"Kartic" <ka******************@gmail.com> wrote:
Your line :
if (!cookie == null) {

should read:
if (!$cookie == null)
(You missed the $ before the cookie variable name)

Thanks!

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.