FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php page-refresh/redirect/boot help needed please





DoctorBeaver
I want to incorporate a simple membership system. I've got the login box (username & password) on the index page. I want the following to happen depending on the status of the database call:-

1) If the login is successful, I want to redirect to the members' front page.

2) If the login is unsuccessful, I want to refresh the page so that I can display a "login failed" type message underneath and allow the visitor to try again (I already know how to display items conditional on a variable's value, so that part isn't a problem).

3) If the IP is suspended, redirect to a page where the visitor can email me to apply to be re-instated (this, obviously would just be a variation on a successful entry re-direct).

4) If the IP is banned, boot them off the site completely.

I've designed a database to hold the userdata. I can handle the access & verification, but I'm not sure how to handle the refresh/redirect/booting. Anyone got any ideas how I can do it?

Any help much appreciated.
Nyizsa
Do you want to alert the user that he is being redirected or just want to make him go away? In the latter case simply use this:
Code:
if ([the IP is banned]) header('Location: about:blank');

So a banned user will be redirected to an empty window.
I hope it helped.
DoctorBeaver
Nyizsa - There's no need for the user to be told if the IP is banned. It's just a "get out of here" boot. I can check on page load if the IP is banned and then do what you suggest with the blank page, or maybe a mock "404 URL not found" type page.

Will header( 'Location: about : xxxxxx') work for any re-direct I want? If so, that's great and my only problem is the refresh. I've already tried calling the same page with an on-click and it didn't refresh.
Nyizsa
If you send the header, it will redirect the browser immediately. You can put anything after Location:, a 404 page, or about:blank - as you prefer.
But there is one thing you have to be careful about: do not send ANY output before sending the header!!
Good luck!
DoctorBeaver
Thanks. However, I've still got a problem. Where do I put my validation function? I'm using a standard HTML form with a submit button.
raver
about refreshing... try reading about the $_SERVER['PHP_SELF'] superglobal variable... it holds data and adresses the script itself.. so for example you can have something like this:

if(isset $_POST['name'])
{
//check your data... for example
//check for illegal characters
//see if the users credentials match
if ([ALL IS GOOD])
{
header(Location: memberspage.php)
}
else
{
?>
<form action = "<? print $_SERVER['PHP_SELF']; ?> method = "post">
//bla bla bla.. insert login box here and the rest of the page
Hope this helped Cool
DoctorBeaver
Thanks raver, I'll give it a go.

UPDATE
No joy. I'm getting a parse error. Unexpected ':' in

header(Location: $index_page);

the full section of code is:-

Code:

<?php
// this routine checks if a username and password have been entered
// if they have, it calls the checking routine and sets the next page
//accordingly

// set initial values as non-member
$is_member = false;
$next_page = "index.php";

// now do the checks
if($_POST['username'] != "") // only perform this routine if a username has been added
 {
 $is_member = test_member($_POST['username'], $_POST['password']); //pass name and pword for checking

 if ($is_member = 1) // user is a valid member so set next page value
   {
   $next_page = "memberindex.php";
   }
 else
   {
   if ($is_member = 2)
     {
     $next_page = "banned_ip.php";  //IP is banned so kick them out
     }
   }
 header(Location: $next_page);     //load the appropriate page
}
?>

// form stuff follows


If I can get this working, I won't need to add a "You do not have access" routine on every page. Members get an extra menu so non-members can't even get at the members-only pages. That way, all I have to do is check $is_member (global variable) when loading the public pages to determine whether the members menu should be displayed.

I've already written the routine for setting the href in the "Home" menu link to the appropriate index page, and that works fine.
cafefort
Hi,

Got some suggestions:

1)The use of cookies and header function MUST be before any output to the browser. This means you can't write earlier in your script something like

echo "some sentence";
...
header(Location: $index_page);

2) some code corrections:
beware the "=" and "==" confusion.

Quote:
Code:
<?php
// this routine checks if a username and password have been entered
// if they have, it calls the checking routine and sets the next page
//accordingly

// set initial values as non-member
$is_member = false;
$next_page = "index.php";

// now do the checks
if($_POST['username'] != "") // only perform this routine if a username has been added
 {
 $is_member = test_member($_POST['username'], $_POST['password']); //pass name and pword for checking

 if ($is_member == 1) // << here
   {
   $next_page = "memberindex.php";
   }
 else
   {
   if ($is_member == 2) // << here too
     {
     $next_page = "banned_ip.php";  //IP is banned so kick them out
     }
   }
 header(Location: $next_page);     //load the appropriate page
}
?>

// form stuff follows



3) Related to the 1st thing I pointed:
isn't the fucntion test_member($u,$p) displaying something? (debug code for example?) this will prevent header(location:..) from working fine.

Hope this helps Smile
DoctorBeaver
Quote:
3) Related to the 1st thing I pointed:
isn't the fucntion test_member($u,$p) displaying something? (debug code for example?) this will prevent header(location:..) from working fine.


No. At the moment it only set the value of the return variable.

Oops @ = and ==... I should know better! *raps own knuckles*

Quote:
1)The use of cookies and header function MUST be before any output to the browser. This means you can't write earlier in your script something like

echo "some sentence";
...
header(Location: $index_page);


I'm not using any php display commands prior to this. However, what you said puzzles me somewhat as, obviously, the form has to be displayed before the user can fill it in (as it happens, the login form is part of the index page, coded in ordinary HTML, which means the whole page is displayed before the user enters their details).
cafefort
There's another error, sorry, I didn't notice its presence before:
the header() function needs a STRING parameter!
whoops Smile

So the real code may be:
Code:

<?php
// this routine checks if a username and password have been entered
// if they have, it calls the checking routine and sets the next page
//accordingly

// set initial values as non-member
$is_member = false;
$next_page = "index.php";

// now do the checks
if($_POST['username'] != "") // only perform this routine if a username has been added
 {
 $is_member = test_member($_POST['username'], $_POST['password']); //pass name and pword for checking

 if ($is_member == 1) // << here
   {
   $next_page = "memberindex.php";
   }
 else
   {
   if ($is_member == 2) // << here too
     {
     $next_page = "banned_ip.php";  //IP is banned so kick them out
     }
   }
 header("Location: $next_page"); //<< "quotes" here
}
?>

// form stuff follows


about the display stuff you speaked about (sorry for my poor english)
well as i see the form is displayed after the header(..) call

Code:
// form stuff follows


so there's no problem Smile

Hope this helps
DoctorBeaver
OK thanks.

What I meant was that the whole page displays before the visitor enters anything. The routine doesn't do anything unless something is entered. So, by implication, the routine runs AFTER the form has been displayed.

I've changed things around a bit. I've now got "chkuser.php" as the form action rather than incorporating the php routine there.

For testing purposes chkuser.php only contains this routine:-

Code:

<?php
$nextpage = "index.php";
if ($_GET['username'] == "doc")
{
   $nextpage = "memberindex.php";
   }
header("Location: $nextpage");
?>


Guess what... IT WORKS! Very Happy

Thanks for all your help.
cafefort
never mind Smile (in french: "de rien! je vous en prie")
hope this the right word in english.

Well, i guessed what you meant, the reason is this conditional:

Code:
if($_POST['username'] != "")


This means don't process until the user posts a username via the form.

I think the only problem was that string parameter to give to the function header()

Is it working fine, now?
DoctorBeaver
Je suis obligee, mon brave.

It worked once, now it's kaput again. I@m trying to work out what I've done to it. Sad

UPDATE

Got it. I added a few if... else... statements to the chkuser routine and I'd got 1 too many {
It works great now. I've got it going to admin, member, non-member or banned page depending on what name I enter. All I need do now is change the chkuser function to check the username/password against the database - & that bit's easy.
Thanks again

Just a couple more questions.

1) Does the header() function wipe the $_GET variables? It goes to the right page but when I try to display the username using echo $_GET['username'] it comes out blank.
Nyizsa
DoctorBeaver wrote:
1) Does the header() function wipe the $_GET variables? It goes to the right page but when I try to display the username using echo $_GET['username'] it comes out blank.

Yes, it wipes $_GET. However, you may fake it:
Code:
header('Location: http://www.yoursite.com/chkuser.php?username=doctorbeaver');

DoctorBeaver wrote:
2) If I can't display anything prior to the header(), how do I display an error message?

"You can't display anything prior to the header()" IN THAT PAGE!!! So this rule applies to every single page, not to the whole session.
DoctorBeaver
OK. I'll have to re-work my page relationships. Thanks for your help.
cafefort
Quote:
"You can't display anything prior to the header()" IN THAT PAGE!!! So this rule applies to every single page, not to the whole session.


Well, actually you can! Smile but by using a quick n' dirty trick that i didn't like myself. It's like using "goto"s instructions in a procedural program, that makes the code ugly.

Anyway, if you really really need to display something before header() call
you can use the buffer functions:
take a look at thses two functions:

1) Send all your displaying to a buffer, before actually displaying:
http://www.php.net/ob_start

2) Flush the buffer and show the displaying
http://www.php.net/ob_end_flush

That's how to use these two functions

Code:
<?php

ob_start(); << here
...
echo "my stuff";
..
header("location:wonderland.php");
..

ob_end_flush(); << here

?>



Hope this helps Smile
DoctorBeaver
Cafefort - thanks yet again. I don't need it now, though, as I've re-worked the pages.

I've still got the login routine on the first page, but the validation checks are on another page called by the Login button. All messages are displayed by page 2. There's a Submit button on the page and I change the action and button label according to what happened in the validation routine. Doing it that way I've avoided using header() altogether now; pages are loaded via buttons.

I've done a similar thing with the registration form . When the user clicks the Register button, it calls another page that does all the database checking & writing, displays error messages, etc.

It's actually a lot easier doing it this way. The only drawback is that there's 1 extra page to load each time. But the validation pages are only a few Kb so it doesn't really take that much longer.

The reason I'm doing all this is that I want a membership system on the site. I've looked at a lot of ready-made packages but either they don't do what I want or they are so complex it would take forever to alter them.

Once I've learned the proper syntax etc for php, I don't think it'll be too difficult to implement. The only part of it I can see being a problem is the private messaging. Letting the members write messages and post them is no problem. It's also no problem if the recipient is not on line; any messages would just sit in the database until accessed. The problem is instant, 2-way messaging.

I'm not sure what the best way is to go about that. Even if I use a simple chat system, I still have to let the person know that someone wants to chat with them, and then put them both into the same room.

If you've got any ideas on that, I'd love to hear them.
cafefort
DoctorBeaver, that's always a pleasure! you're welcome Smile
Well about your problem, I guess it's a conceptual one. Here's how it goes:

A member A want to chat with another member B
the messaging is stored say in a string M.

So we have this case:

1,n
Member <------------------>message
1,n

So according to this schema you have to add a table named "message"
which will contain say

member1_id
member2_id
message_text
message_date

and then with an ajax simple script you can update the "message" table
everytime a member enters a message.

Hope this helps Smile

P.S

Quote:
Je suis obligee, mon brave


Oh, mais tout le plaisir est pour moi milady! Smile
DoctorBeaver
Milady? Zut alors! Je suis un homme!!

I'm a bit hesitant to use Ajax as, I believe, it uses Javascript. Not everyone has Js enabled. If I can't find any other solution, that's probably the route I'll take.
cafefort
Quote:
Milady? Zut alors! Je suis un homme!!


Sorry milord! Smile But that was in part your fault : in french
an we add a second "e" to the end of an adjectif if its about a femal Wink
French people need that distinction in sex Wink

Notice: I'm not french, but I do speak it perfectly, say at least better than english.

So you should write:
Je suis oblige (with the accent &eacute; )

Anyway, you can do it without ajax of course,
but you'll have to reload the page at every request.
Imgine a while all the traffic that it implies!

IMHO, you should add a <nonscript> tag to your chat page showing
in BIG RED an error message to the user to tell him that he sould enable javascript and if you're kind enough Wink tell him how to, by pointing him to
some page on the subject.

Hope this helps and good luck.
Don't hesitate to ask, It's a real pleasure Smile
DoctorBeaver
I did wonder whether obligee was correct. I had my doubts.

I'm inclined to agree with you... if they want to use instant messaging, they have to have Js. I think that's fair enough. It would certainly make my life a lot easier & I could get it up & running a lot quicker that way.

Merci encore, & salaam.
cafefort
De rien Smile Tout le plaisir est pour moi Smile

Indeed, I do really think that if they want chat, they could AT LEAST enable
javascript, that's not that hard after all. A little effort by thier side isn't harming Wink

Thanks for "Salaam", salaam to you too.

P.S. You can say "Azul fellak" it's a kabyle sentence (my mother's tongue)
which almost means "hello ".
DoctorBeaver
cafefort wrote:

P.S. You can say "Azul fellak" it's a kabyle sentence (my mother's tongue)
which almost means "hello ".


Almost hello? I think I understand. There's an expression in Luganda - Wasosoti? (I think that's how it's spelt). The literal translation means "Did you sleep/dream well?", but it's used as a greeting in the morning. No-one takes it literally.

I've found a PERL instant messaging system & I'm looking into that a bit further at the moment.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.