By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

Forwarding to next php script on correct input?

P: n/a
Hi!
I need the best way to solve a problem, I'm a newbie.

I have "login.php", a login script. That page first displays a login
form with itself as target of a post-request to validate data when
user presses the login-button (I can't change that setup).

If the login is not correct I want to display the login form again and
show an additional line displaying the error, that is easy and already
working.

But if the login was correct I want to display another page to the
user that is constructed in "main.php".

Now what is the best way (easiest and most compatible) when I'm in
"login.php" to switch to the next page "main.php"?

Thank you very much for your answers!
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Joe Randstein wrote:
Hi!
I need the best way to solve a problem, I'm a newbie.

I have "login.php", a login script. That page first displays a login
form with itself as target of a post-request to validate data when
user presses the login-button (I can't change that setup).

If the login is not correct I want to display the login form again and
show an additional line displaying the error, that is easy and already
working.

But if the login was correct I want to display another page to the
user that is constructed in "main.php".

Now what is the best way (easiest and most compatible) when I'm in
"login.php" to switch to the next page "main.php"?

Thank you very much for your answers!


This must come before any output at all:
header('Location: main.php');
Jul 17 '05 #2

P: n/a
Kevin Thorpe wrote:
This must come before any output at all:
header('Location: main.php');


Of course, according to the RFCs, you have to add the domain name:

header('Location: http://www.example.com/main.php');
exit;

Matthias
Jul 17 '05 #3

P: n/a

"Matthias Esken" <mu******************@usenetverwaltung.org> wrote in
message news:c6**********@usenet.esken.de...
Kevin Thorpe wrote:
This must come before any output at all:
header('Location: main.php');


Of course, according to the RFCs, you have to add the domain name:

header('Location: http://www.example.com/main.php');
exit;

Matthias


He's right! RFC 2616 section 14 (Head Field Definitions):

14.30 Location
The Location response-header field is used to redirect the recipient to a
location other than the Request-URI for completion of the request or
identification of a new resource. For 201 (Created) responses, the Location
is that of the new resource which was created by the request. For 3xx
responses, the location SHOULD indicate the server's preferred URI for
automatic redirection to the resource. The field value consists of a single
absolute URI.

Location = "Location" ":" absoluteURI
<snip>

Prepend $_SERVER['host'] (and /) to your relativeURI:

<?php

header("Location: ".$_SERVER['host']."/index.php");

?>
Garp


Jul 17 '05 #4

P: n/a
Matthias Esken <mu******************@usenetverwaltung.org> wrote in message news:<c6**********@usenet.esken.de>...
Kevin Thorpe wrote:
This must come before any output at all:
header('Location: main.php');


Of course, according to the RFCs, you have to add the domain name:

header('Location: http://www.example.com/main.php');
exit;

Matthias


Thanks to both of you!

More questions:

-so far I have programmed so that I can use relative URLs in my
scripts, so I can move the scripts to a subdirectory or other domain
and do not have to care. But if you say I should add domain name and
directory to the Location-header, then what is the easiest way to get
that without hardcoding it?

-Would this be an alternative to the location-header? Last night
(before I read your replies) I thought about turning on buffering at
start of my login.php. When I would find out that the given data by
user was correct, then I would empty the buffer and include() my
main.php and then don't give any more output by login.php.
Shouldn't the user then see only output from main.php? What could be
difficulties with this solution?

But I will now try first the Location-header. It seems easier ;-)
Jul 17 '05 #5

P: n/a
Joe Randstein wrote:
More questions:

-so far I have programmed so that I can use relative URLs in my
scripts, so I can move the scripts to a subdirectory or other domain
and do not have to care. But if you say I should add domain name and
directory to the Location-header, then what is the easiest way to get
that without hardcoding it?


Usually I tackle this by using a global settings.php.inc-file (located
below the www-root directory), which sets the common configuration
settings for the scripts (paths, database settings and so on). This way
you can easily change the path-specific settings if necessary.

So in settings.php.inc you could have:
$config["domain"] = 'http://www.example.com/';
$config["path_root"] = 'scripts/';

and in the program files you could use:
include('/home/www/mydir/settings.php.inc');
header('location: ' . $config["domain"] . $config["path_root"] .
'myscript.php');

HTH

--
Suni

Jul 17 '05 #6

P: n/a
In article <40***********************@news.song.fi>, Juha Suni wrote:
Joe Randstein wrote:
More questions:

-so far I have programmed so that I can use relative URLs in my
scripts, so I can move the scripts to a subdirectory or other domain
and do not have to care. But if you say I should add domain name and
directory to the Location-header, then what is the easiest way to get
that without hardcoding it?


Usually I tackle this by using a global settings.php.inc-file (located
below the www-root directory), which sets the common configuration
settings for the scripts (paths, database settings and so on). This way
you can easily change the path-specific settings if necessary.

So in settings.php.inc you could have:
$config["domain"] = 'http://www.example.com/';
$config["path_root"] = 'scripts/';

and in the program files you could use:
include('/home/www/mydir/settings.php.inc');
header('location: ' . $config["domain"] . $config["path_root"] .
'myscript.php');


If the location of that settings file changes, you still have to update
all those files. In this case auto_prepend can be handy ;)
--
http://home.mysth.be/~timvw
Jul 17 '05 #7

P: n/a
In article <c6************@ID-188825.news.uni-berlin.de>,
Tim Van Wassenhove <eu**@pi.be> wrote:
So in settings.php.inc you could have:
$config["domain"] = 'http://www.example.com/';
$config["path_root"] = 'scripts/';

and in the program files you could use:
include('/home/www/mydir/settings.php.inc');
header('location: ' . $config["domain"] . $config["path_root"] .
'myscript.php');


If the location of that settings file changes, you still have to update
all those files. In this case auto_prepend can be handy ;)


What I do is use one config file which sets all the paths and is located
in . (i.e. in the same directory as the rest of the scripts), so that it
is always findable in a default PHP configuration. I include that file
in every script. So if the location of settings files etc. changes, I
only have to update my path_config.inc.php file.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #8

P: n/a
In article <de***************************@news1.news.xs4all.n l>, Jan Pieter Kunst wrote:
In article <c6************@ID-188825.news.uni-berlin.de>,
Tim Van Wassenhove <eu**@pi.be> wrote:
> So in settings.php.inc you could have:
> $config["domain"] = 'http://www.example.com/';
> $config["path_root"] = 'scripts/';
>
> and in the program files you could use:
> include('/home/www/mydir/settings.php.inc');
> header('location: ' . $config["domain"] . $config["path_root"] .
> 'myscript.php');


If the location of that settings file changes, you still have to update
all those files. In this case auto_prepend can be handy ;)


What I do is use one config file which sets all the paths and is located
in . (i.e. in the same directory as the rest of the scripts), so that it
is always findable in a default PHP configuration. I include that file
in every script. So if the location of settings files etc. changes, I
only have to update my path_config.inc.php file.


This is what i used to do before.

But the difference with .htaccess is that .htaccess also works for
subdirectories. And with a init.php you end up including
.../../../init.php in the scripts that are in subdirectories.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.