0

I am wondering why this is not relocating me to the contact page?

    <?php session_start(); ?>


    <?php

    function setSessions($page){
        switch($page)
        {
            case "contact":
                $_SESSION['contact_name'] = $_GET['name'];
                $_SESSION['contact_address'] = $_GET['address'];
                $_SESSION['contact_phone'] = $_GET['phone'];
                header('Location:contact.php');
                exit;
                break;
            case "employment":
                break;
            case "position":
                break;
            default:
                break;
        }

    }




    $current_page = $_REQUEST['page'];
    setSessions($current_page);


    ?>

I am positive it is going in the contact case. thanks for the help

-edit: NVM, it works. Thanks!

5
  • NO output before a header, you should be getting an error Commented Feb 5, 2013 at 3:15
  • you are outputting a string, before the header that should be causing an error. You have error checking\display off which is a bad way to develop code Commented Feb 5, 2013 at 3:15
  • -post edit, still error checking must be off, so how would know if you got an error? Commented Feb 5, 2013 at 3:18
  • That white space is still output. just combine that all into one php enclosure Commented Feb 5, 2013 at 3:18
  • 1
    in addition you should use the full URI in header location not a relative one Commented Feb 5, 2013 at 3:20

6 Answers 6

2

You can't send any output before making a call to header().

You would have to remove:

<html>

and

echo "i got in here";

References:

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

Comments

1

This is why:

 <html>

You can't call header() if you have output anything to the browser. Move the redirect code above this html tag and remove that echo and you should be ok.

1 Comment

such a bad habit to get into though. just following the normal conventions would be a better approach to php
1

For future reference white html space before header being called will also stop this from working. (lines 2 & 3)

1 Comment

4 answers and 3 comments saying the same thing, but feel free to add your answer 20 minutes latter anyway :-)
0

You can't have ANY output what so ever before using header(). The echo statement in your switch statement is considered output and is the thing messing up your code.

Comments

0

Any emission sends headers in PHP, and once you've sent headers you can't send any more. Even whitespace will do this, but you have the string <html> being output. You can't do that.

The better solution is to not do it and save all emission until after you have built everything that you want to output.

The worse solution would be to use output buffering. At the start of your script but, ob_start(). Then, headers will work as normal.

2 Comments

Okay, so i've taken out all the output, and now its just going to a blank page.
Are you sure it's not redirecting? Or do the pages you're redirecting to just not have any content?
0

The problem is that you are not getting the correct value for $current_page; you can echo that value and you will see it's empty. So what you need to do is you should take the current page value here using this function:

function getMyurl(){

  $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

  echo $url;

}

now $current_page=getMyurl();

You should know that getMyurl will return a full url of your website or application so you should modify it in your cases where it's written contact you should replace that with case "yourwebsite/contact".

It should now work for you

Comments

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.