Opening another page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php14user
Forum Newbie
Posts: 12
Joined: Tue Apr 08, 2008 11:02 pm

Opening another page

Post by php14user »

Hi,

I have a login page and when I submit the login form, login page reloads and include a php file "get_valid_user.php" that has the code for checking whether user exists or not.
Now when user does not exists, "get_valid_user.php" echoes the message on login page that user does not exists.
My problem is how to make "get_valid_user.php" open a new page "user_info.php" if user has supplied valid credentials?

I tried following code:

Code: Select all

<?php
 
    if($_POST["submit_form"] == "login_form_submitted")
    {
           if(!isset($_FILES) && isset($HTTP_POST_FILES))
           {
                  $_FILES = $HTTP_POST_FILES;
           }
          
           $con = mysql_connect("localhost","root","xyz");
           if (!$con)
                 {
                           die('Could not connect to MySQL: ' . mysql_error());
                 }
 
           mysql_select_db("my_db", $con);
 
           $login_id = $_POST["login_id"];
           $login_password = $_POST["login_password"];
 
           $result3 = mysql_query("SELECT * FROM Company_Registration where login_id = '$login_id' && login_password = '$login_password'");
 
           $no_Of_Row_Affected = mysql_affected_rows();
 
           if ($no_Of_Row_Affected == 0)
               { 
                         echo "No user!!";
               }
           else
               {
 
                          [color=#008000]//header('Location:http://localhost/Site/user_window.php');[/color]
                          [color=#008000]//include("user_window.php");[/color]
                          [color=#0000FF]I need to include a code here to open a new page user_window.php.[/color]
                }
}
?>
FYI, when I use "include("user_window.php");" then the content of user_window.php and content of home page overlaps.
I want to open the content of user_window.php in a separate page.

And when I use "header('Location:http://localhost/Site/user_window.php');", I get following error message:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/Site/home_page.php:5) in /Library/WebServer/Documents/Site/get_valid_user.php on line 40

Your help will be appreciated.
Thanks.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Opening another page

Post by Phoenixheart »

header('Location') is often used in such a case. You're getting a warning because you've just output something (a text, or even a white space).

As alternatives, you can use javascript (not recommended though):

Code: Select all

<script>window.location.href="http://your.new.page.php";</script>
or meta tags:

Code: Select all

<meta http-equiv="refresh" content="1;url=http://your.new.page.php">
But, as I said, header is preferred :D .
php14user
Forum Newbie
Posts: 12
Joined: Tue Apr 08, 2008 11:02 pm

Re: Opening another page

Post by php14user »

Hi,

Could you please review my code shown above? (Please assume that I am using "header('Location')" method in else part)
Please let me know what is causing below error:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/Site/home_page.php:5) in /Library/WebServer/Documents/Site/get_valid_user.php on line 40

Is my code correct?

Thanks,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Opening another page

Post by onion2k »

header() will only work if you use it before outputting any HTML. You need to rethink the flow of your code (eg, move the header() call to somewhere near the top).
Post Reply