Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Yet another PHP beginner that can't use the header function

Status
Not open for further replies.

mpopnoe

Programmer
Feb 28, 2002
47
0
0
US
I've read all the suggestions on this site for using the header function properly and I'm still having trouble. I know that the header function must be called before any output is sent but I should be able to set session variables before calling the header function right? What I am trying to do is obtain the username and password from the user, check it against the valid usernames/passwords in my database, and redirect them to the first page of my site. Each page after login checks the session variable to ensure the user logged in properly. The very simple code snippet below does not work:

<?php

//SET THE VALUES FOR TESTING PURPOSES
session_start();

session_register(&quot;loggedin&quot;);
$loggedin = true;

header(&quot;Location:
?>

this is the error:

Warning: Cannot add header information - headers already sent by (output started at c:\inetpub\ in c:\inetpub\ on line 9

Can I not assign the session vars before I call the header? Is there a better/another way to redirect? The only way I could get the header function to work was by having nothing else but that function:

<?php
header(&quot;Location: ?>

So I know the header function is working properly on my system. Any help is greatly appreciated.

thanks
 
I'm not sure if this would be your problem or not, but I don't think the semicolon inside the header() function is necissary, only the one after it. If all else fails, you can use javascript for a redirect

<script>
location.replace(' </script>

Hope this helps
 
The extra semi colons are put in from this form for some reason, the are not in my code. Thanks for the help though, I might use javascript instead.

thanks!
 
Do you see a line at the top of the page that looks something like this:
<!DOCTYPE html public &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

If so, that's your header info, delete it, or move it to below your php code ( into the html ).

 
I think everyone is diagnosing this wrong. This code works for me:
---------TOP OF PAGE HERE---------------------
<?
session_start();
header(&quot;Location: ?>
----------------------------------------------

Make sure you're not doing this:
-----------TOP OF PAGE------------------------

<?
session_start();
header(&quot;Location: ?>
----------------------------------------------
In the 2nd example theres a blank line before the php code starts. Make sure there is no blank line!

To answer your question, YES you can use session_start() and the header function.

I'd like to make a note, that in the newest version of php, which you probably have, they've included a way to BUFFER output, as to avoid the big header annoyances. Many people forget about this little trick. It is in the php manual under the discussion of the header function. You'll have to read up on how to buffer the output yourself though, I learned through a tutorial I found by searching google. Good luck!

--joe
 
I agree with joe. I spent a long time looking for that extra space at the beginning of my code.

CJB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top