0

I am trying to figure out the best way to do this and probably totally over thinking it.

I need users to submit their email before they can get the page contents.

they don't need to confirm at this point just submit any email. So I created a simple form with an email field and submit it to my process page using "POST"

However, I need them to be redirected to a dynamic url after submitting their email.

So I want to be able to put a variable in the url to the page I want to redirect to like this:

http://myurl.com/provide_email.php?url=newpage.html

On page provide_email.php I put :

<?php
session_start();

session_register('url');

$_SESSION['url'] = $_GET['url'];

?>

and echo the ouput on the same page (just to test it). So, on the same page it shows:

newpage.html

Then on my process.php page I put:

<?php

session_start();

$dynamicurl = $_GET['url'];

if(empty($_GET))
    echo "No GET variables";
else
    print_r($_GET); 


$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'folder1/folder2/';
header("Location: http://$host$uri/$extra/".$dynamicurl);


?>

However every time I submit the page it will not redirect to the url with the $dynamicurl variable.

I have also tried :

header("Location: http://$host$uri/$extra/$dynamicurl");

What am I doing the wrong?

The url has to be dynamic because I am creating the pages from a cms that ultimately the user will go to. I just want them to have to submit an email before they can get to each unique page.

3
  • $dynamicurl = $_GET['url']; why? you were trying to save the data into session variable, why are trying to get something from the query? session_register is DEPRECATED since php 5.3 Commented Feb 7, 2012 at 0:03
  • Have you tested what http://$host$uri/$extra/".$dynamicurl actually results in? Commented Feb 7, 2012 at 0:03
  • $dynamicurl = $_GET['url']; What si the use of this statement . I didn't find someting is passed in GET array , from query string Commented Feb 7, 2012 at 11:32

1 Answer 1

0

Your debug statement is the problem:

if(empty($_GET))
    echo "No GET variables";
else
    print_r($_GET); 

will start output, so the following header() cannot be honoured. I am sure your log is full of warning: Cannot set header, output already started

Use

if(empty($_GET))
    $message "No GET variables";
else
    $message=var_export($_GET,true); 

and

echo $message;

AFTER the header() statement.

Sign up to request clarification or add additional context in comments.

4 Comments

I put the debug statement just to see what was going wrong, but yes ...it is saying ... warning: Cannot set header, output already started . But if dont have the if statment it WILL redirect but only to the directory: http://myurl.com/folder1/folder2/ and never to the ending URL (newpage.html) If there is a better way to do this I am open. I just don't know any other way.
It shows me : Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/webserver/public_html/test_site/process2.php on line 9 but line 9 is what I copied from your post line 9 and 11 are code highlighted as an error: if(empty($_GET)) $message "No GET variables"; else $message=var_export($_GET,true));
I found my answer .. but new user can not answer their questions apparently. [stackoverflow.com/questions/722836/… This seems to have worked... I removed the debug altogether and chnaged the code to this : header("Location: http://$host$uri/$extra/"."$_SESSION[url]"); [1]: stackoverflow.com/questions/722836/…
I edited my answer: There was a superfluous bracket in line 9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.