Form: Submits to self on any 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
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Form: Submits to self on any page?

Post by jonas »

I'm looking to work on my login script today for my site.... it is in the header of every page on my site.... check it out
http://www.bolt3.com/systems.php

So, the top right will be where they can login. So I know this is probably simple but I searched for a bit and haven't found a solid answer but how do I get the form to submit to itself regardless of what page it's on?

So if they login on systems.php it will submit to systems.php but if they are on forums.php it will know to submit to forums.php and not to systems.php

I'm putting the login check in the header so it will be on every page but I just need to know how to make a form submit to itself.

Is it PHP_SELF or something... I know it's probably quite easy. :roll: :roll: :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP_SELF should be it.. although it's not guaranteed to exist, so check your server variables to make sure it's there.. if it's not there, $_SERVER['SCRIPT_NAME'] should be there...
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

<form action="" method="POST">

There is no need for PHP_SELF I don't think!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

true, in a lot of browsers... but I not entirely sure all browsers will work without action...
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

I use this:

Code: Select all

<form action="?">
( i don't know if it's correct to do that, but it works )
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Use $_SERVER['PHP_SELF']...

In your PHP do something like...

Code: Select all

<?php
$form_action = $_SERVER['PHP_SELF'];
?>
... and call it like this ...

Code: Select all

&lt;form action="&lt;?= $form_action ?&gt;" method="post"&gt;
This is just one way (of many possible ways) to do this. Works pretty well for posting to the same script your app is running.

Something to remember (this may or may not affect your needs) but the PHP_SELF server variable always carries with it the leading forward slash and directory path from root, ie:

scriptname: myapp.php
location: root
PHP_SELF: /myapp.php

scriptname: otherapp.php
location: /test
PHP_SELF: /test/otherapp.php

Hope this helps :D
Post Reply