Link to home
Start Free TrialLog in
Avatar of iambg
iambg

asked on

PHP or Javascript redirect problem

Hi there, just joined up.

I have an interesting issue. What I am trying to accomplish is a redirect ONLY if a cookie does NOT exist. When users hit an entrance page, there is a list of links to different pages. I want those pages accessed only if they have submitted a form. If that form has been filled out, a cookie is set (no real values) and the user is forwarded back to the entrance page and can open the links on that page with no problem.

Is this possible?

HEre is the code I have been using:

The form thank you page (upon submit)

<script>
<!--

// page to go to if cookie exists
go_to = "../entrance_page.html";

// number of days cookie lives for
num_days = 60;
function ged(noDays){
var today = new Date();
var expr = new Date(today.getTime() + noDays*24*60*60*1000);
return expr.toGMTString();
}

function readCookie(cookieName){
var start = document.cookie.indexOf(cookieName);
if (start == -1){
document.cookie = "bmed=yes; expires=" + ged(num_days);
} else {
window.location = go_to;
}
}

readCookie("bmed");
// -->
</script>



On the pages linked off the entrance page:

<script>
<!--
// page to go to if cookie does not exist
go_to = "download-form.html";

function readCookie(cookieName){
    var start = document.cookie.indexOf(cookieName);
    if (start != -1){
        document.cookie = "bmed=yes; expires=" + ged(num_days);
    } else {
        window.location = go_to;
    }
}
}

readCookie("bmed");
// -->
</script>


My issue is that the pages forward to the download form no matter what.
Avatar of pghn
pghn
Flag of Portugal image

You need to create a cookie when an user submit the form (1), and then on your specific page, check if that cookie exists or not, to act based on it...(2)



PHP code will be something like this:
/* (1) Create the cookie where the user fills up the form */
<?php
$expire=time()+60*60*24*30;
setcookie("SubmittedForm", "True", $expire);
?>
 
 
<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['Testcookie'])){;
//code case cookie not set
} 
//code case cookie is set
?>

Open in new window

Sorry on the code the line
if(!isset($_cookie['Testcookie'])){;

should be:
if(!isset($_cookie['SubmittedForm'])){;


I forgot to put the name of the expected cookie.
If you're using JavaScript, add this at the top of your page that you want to redirect away from.
// page to go to if cookie exists
go_to = "../entrance_page.html";
 
var x = readCookie('cookieName')
if !(x) {
	window.location = go_to;
}

Open in new window

For PHP - to do the actual redirect the code would look like this.  Note that this would have to be the very first thing that executes in that script.  If anything is defined first, like the <HTML> header, it won't work.  However, I have a feeling you'll be sticking with JavaScript.
<?php
if !(isset($_COOKIE["cookieName"]))
  header( 'Location: http://www.new_url.com/new_page.html' ) ;
?>

Open in new window

Avatar of iambg
iambg

ASKER

Ok, I have this on one of my linked pages (which I then want viewable after the form is submitted):

<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){;
//code case cookie not set
} header("Location: download-form.html");
//code case cookie is set
?>

Then on the form Thank you page:

<?php
$expire=time()+60*60*24*30;
setcookie("bmed", "True", $expire);
?>

My issue is still that after I submit the form and am taken to the entrance page, I click for the same page again (with the redirect script) and it still redirects me. I would like to make that page viewable now.

So some sort of condition that if the cookie is set, stay on the page, don't redirect.
I think I see your issue.  Your IF statement's structure is not correct.  The result is that your line to redirect runs every time that page is loaded.  You just need to move your bracket and semicolon a bit.  Try this:
<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
}
?>

Open in new window

Update to the above statement.  You don't need an "else" statement because any code listed below that IF statement will not run if the redirect line gets executed.  However, if you DID want to have an "else" clause, it would look like this:
<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
} else {
//code case cookie is set
}
?>

Open in new window

Avatar of iambg

ASKER

Synx, just changed my page to this code:
<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
}
?>

It still redirects no matter what...
Avatar of iambg

ASKER

So in a nutshell all I am trying to accomplish is allow the user to stay on a page if the form has been submitted, if not redirect to download-form.html...
That telsl me that the cookie is not being set... Do you know for sure that the cookie is being set correctly when the form is submitted?  Can you paste the code that you're using to set the cookie?

If you're using JavaScript to set the cookie, why not use JavaScript to check for its existence?  Did you try the JavaScript that I listed earlier?  I'll paste it again here.  Let me know if that works:
// page to go to if cookie exists
go_to = "../entrance_page.html";
 
var x = readCookie('bmed')
if !(x) {
        window.location = go_to;
}

Open in new window

Avatar of iambg

ASKER

It's being set. I can check in my browser and see it there.

I tried Javascript as well and got this far. Same issue, page always redirects.

HEre's the thing, the entrance page is just my start point. I don't want to forward or redirect to any pages UNLESS they don't have a cookie which in that case they would be redirected to download-form.html
<?php
$expire=time()+60*60*24*30;
setcookie("bmed", "True", $expire);
?>

Open in new window

Right - that's how the code samples work, if no cookie... then redirect.  In this PHP code that you tried below, I'm not positive - but I think it's a problem that there's nothing separating the "!" and the "isset" function.  I think you should move the "!" to the outside of the first parenthesis.  So instead of the way it is below, it starts with "   if !(isset($_cookie['bmed'])){   "

<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
}
?>

Does that make a difference?  I'm going to think about this some more... I was going to suggest that maybe your browser is not set to accept cookies, but obviously it is since you can see it in your browser.
Avatar of iambg

ASKER

Unfortunatley still the same issue.

Thanks for all the help, definitely let me know if you have any other suggestions.

I am so stumped on this one. Everything I have tried leads me to this point.

Kind of tearing my hair out over this...
Bummer!  It's probably something small - it usually is.  Just out of curiosity... could you replace the redirect line with some code to just write a string to the screen so you can verify where in the IF statement you're going?  Something like:

<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
  echo "Cookie is not set!";
} else {
  echo "Cookie is set!";
}
?>

Then access it a few times and make sure it's printing the correct string depending on whether or not a cookie was set.
Another question - are you calling the header() function anywhere else in your code?
Another thing - when I've used header() to redirect, I've always followed it with an "exit()" command.  I'm looking online and see that's standard practice.  What if you try this:

<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
 exit();
}
?>

Open in new window

Avatar of iambg

ASKER

I tried this quickly and could not see any results. Just a blank page...

I will try your next suggestion.

Thanks!
Avatar of iambg

ASKER

Arghhh.

Same thing.

Shucks.  I think I'd go back to that IF loop and make sure it's evaluating correctly.  I think you said you tried the code with the "echo" statements and got blank pages.  I think you should look at that and try to get confirmation that when you have a cookie set, you are falling in the correct place in the code.  It appears that it's always falling into that first part - so it's always thinking that the cookie is set.

Can you try again with:
<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
  echo "Cookie is not set!";
} else {
  echo "Cookie is set!";
}
?>

If you are still getting blanks - could you attach the code for the entire page in a code snippet?  Maybe if I can see the code as a whole something will stick out...
Avatar of iambg

ASKER

I am adding this:

<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
  echo "Cookie is not set!";
} else {
  echo "Cookie is set!";
}
?>

To the top of a regular HTML page, but of course it has a .php file extension. I can still paste the code to the rest of the page, if needed.

I even tried just a blank page with this code saved as test.php. Still no results...
Could you attach the entire file that you're using?
Avatar of iambg

ASKER

Crapola, it won't let me attach a PHP file or zip it...

So I changed it to a .txt file that you can just change the xtension.

This IS the page that I have been using your recommended scripts, right now it has the latest echo script that we have been messing with..
equipmentplan-cart-washer.txt
Thanks.  I don't see the PHP code in there, or any mention of "cookie".  Is that the right page?
Avatar of iambg

ASKER

I just opened that file up and was not aware that changing the extension removed the php script.

For what it's worth, I was placing the script at the very top of the file before <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Avatar of iambg

ASKER

HEre's at least a snippit of how I have it set up
<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
  echo "Cookie is not set!";
} else {
  echo "Cookie is set!";
}
?>
 
<!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"><!-- InstanceBegin template="/Templates/index.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Open in new window

Avatar of iambg

ASKER

Here is with the cookie/redirect script


<?php
/* (2) If the cookie is not set do something */
if(!isset($_cookie['bmed'])){
//code case cookie not set
 header("Location: download-form.html");
 exit();
}
?>
<!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"><!-- InstanceBegin template="/Templates/index.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Open in new window

It did??  That's weird.  So, the file begins with this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
/* (2) If the cookie is not set do something */
if !(isset($_cookie['bmed'])){
  echo "Cookie is not set!";
} else {
  echo "Cookie is set!";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/index.dwt" codeOutsideHTMLIsLocked="false" -->
<head>

Open in new window

Avatar of iambg

ASKER

The above returns a blank page...
ASKER CERTIFIED SOLUTION
Avatar of synx
synx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iambg

ASKER

Alright now, we are on to something here...

I insterted the above code, instead of entrance_page.html I put my form: download-form.html

It redirected, appropriately (no pop-up or dialog). I submitted the form, form redirected me to my entrance page, and clicked the link to the page with the above code but it popped up the message saying "Cookie NOT set, BUT

It then let me view the page.

Good stuff, we are real real close I think...
So... if you get rid of the two "alert" lines - delete or comment out -  is it redirecting correctly?  (If you get rid of the second one, you'll have to get rid of the entire "else" clause since that's the only line there.)

It should... if not can you paste the <script> block here showing the new JavaScript that you're using?

So close...
Avatar of iambg

ASKER

I take it back. I must not have cleared my cache entirely before as now, with all cookies cleared, the script does not forward to the form and shows the alert "cookie not set."

However, when I manually browse to the form, submit it and get the cookie, when I browse to the page again (that holds the above script) it pops the alert again "cookie not set" when I can see the cookie in my browser preferences.

Meehhhh
Shoot.  This is probably a no-brainer, but are you positive the cookie is called 'bmed'?

Can you show the JavaScript you are using to set the cookie?
Avatar of iambg

ASKER

Removing the alert in "if" line and putting window.goto gets us back to where we were with the PHP script.

It redirects no matter what.

Is the cookie not being set appropriately?

HEre is my set cookie code:

$expire=time()+60*60*24*30;
setcookie("bmed", "True", $expire);

(It is in the same php script as my form submit code...
Avatar of iambg

ASKER

Would the value True be of any use or hindrance in this situation?
Avatar of iambg

ASKER

Anyone have any other ideas?
Avatar of iambg

ASKER

Gaaaa!

Cookie path was not defined. D'oh!!!!

It appears to be working now. Will test thoroughly in the AM
Excellent!  That was a hair-puller.  Glad it appears to be working!  Good luck with the testing this morning.