Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Matt

Want To Change Logout To Point Back To The Same Page!

Recommended Posts

I am working on a site based on Example 1 from the book. As it is written, when a user logs into

the site a "logout" link is displayed on the sidebar. When the link is clicked, it calls another page,

logout.php, which logs the person out.

 

What I want to do is keep everything modular and never have the user leave the index.php page.

Instead, when a user logs out I would like them to be redirected back to the same index.php page

and have the login form display again.

 

I tried playing with this today, but ran into all sorts of errors! The way I think this should be

handled is that clicking the "logout" link will call another included script in the background which

does the actual logging out. After that the page will be reloaded.

 

Figuring out where exactly to place the following code is the difficult

part:

// Destroy the session:
$_SESSION = array(); // Destroy the variables.
session_destroy(); // Destroy the session itself.
setcookie (session_name(), '', time()-300); // Destroy the cookie.

I'm just not sure if this is the best way to do this, or in fact, how I should do it.

 

I would appreciate anyone's ideas/help with this!

 

Thanks in advance,

 

Matt

Share this post


Link to post
Share on other sites

Hi Matt,

 

Updated:

 

Why can't you just add a redirect after you've logged out that takes the user back to the index page?

 

What kind of errors did you get? :)

  • Upvote 1

Share this post


Link to post
Share on other sites

I seem to recall Larry talking about this in his PHP 6 & MySQL 5 book.

 

I think you use the header function with 'Location: ' to tell the PHP script where to go to next. After logging the user out, you could probably very easily just direct the user back to the index.php script, and the user wouldn't even notice.

 

Of course, if it were me, I think I would want to be redirected to the main page after consciously choosing to log out, regardless of what page I logged out from. Anyway, that's just me.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks for the reply Jonathon!

 

Why can't you just add a redirect after you've logged out that takes the user back to the index page?

That's what I'm starting to think would be the best way.

 

I'm thinking I should have the logout link to another page.

<a href="logout.php?returnpage=". $SERVER['PHP_SELF'].">logout</a>

Then have the following in the logout.php script.

<?php
//Start session
session_start();

$page = isset($_GET['returnpage']);

// Destroy the session:
$_SESSION = array(); // Destroy the variables.
session_destroy(); // Destroy the session itself.
setcookie (session_name(), '', time()-300); // Destroy the cookie.

header("location: $page");
?>

The outline of this code came from a post I found in another forum while doing a google search on this topic.

 

What kind of errors did you get? :)

The errors were caused by a first attempt at doing this. There was a "can't find stream" error for an

included file as well as a "headers already sent" error. I looked at the code after and realized that I

had set it up wrong, so hopefully they were just caused by my initial carelessness.

  • Upvote 1

Share this post


Link to post
Share on other sites

Of course, if it were me, I think I would want to be redirected to the main page after consciously choosing to log out, regardless of what page I logged out from. Anyway, that's just me.

 

HartlySan, yes! The user will be directed back to their gallery index page. I almost forgot about that. With the code above, if they logout while visiting another gallery, they will be redirected to that other gallery's index page after logout.

 

I'm starting to think I need to store each user's gallery url in a session variable. Making a call to the database during logout is simply not acceptable!

  • Upvote 1

Share this post


Link to post
Share on other sites

Your suggested code is something quite similar to what I would have suggested. But as I wrote my post it was quite late where I am. I would think that by keeping the url in a session and then logging out and using the Location redirect as HartleySan correctly referenced would let you achieve your goal. That's how I would have tried to do it. Keep us posted how you get on

 

:)

  • Upvote 1

Share this post


Link to post
Share on other sites

I now have everything stored in session variables and they work fine!

 

However, I have another question: How do you check to see if a field returned in a query is NULL or empty? I did a search on google and found some answers in other forums, but they were all over the place. People were arguing with each other over the best way to do this, and I thought that this should be pretty straight forward.

 

The reason I ask is because I need to do the following check when a user logs in:

 

In the login.inc.php script, I run a LEFT JOIN query on the `users` table and the `profiles` table in order to get the user_id, username, type, and directory_name (of their gallery).

$query = "SELECT users.id, users.username, users.type, IF(users.date_expires >= NOW(), true, false), profiles.directory_name FROM users LEFT JOIN 
profiles ON users.id=profiles.user_id WHERE (users.email='$e' AND users.pass='"  . get_password_hash($p) .  "') LIMIT 0,1";

 

Besides redirecting the user at logout, the other reason I am checking the directory_name field in the `profiles` table is because I want to know if the user has created a gallery yet or not (they are not immediately required to). If they have, I want to store it in a session variable, so that I can create a link to their gallery in the side navigation menu. If they haven't, then I want to add a "Create Gallery" link to the side navigation menu.

 

I already have the directory_name stored in a session variable and it works fine. However, I haven't added the check to see if the returned value is NULL/empty.

 

If anyone knows how to do this, I would really appreciate it!

 

Matt

Share this post


Link to post
Share on other sites

Well for that column, I think I'd allow for a boolean and use the result from the mysql to write the appropriate link. That is assuming they don't need the boolean column to actually provide anything more that a yes or no. If you did need to, you could always set it as a default to NULL (as null and empty aren't the same) and then update the column on creation of the gallery. I think that's how I'd look at doing this :)

  • Upvote 1

Share this post


Link to post
Share on other sites

Doesn't Larry do something like this for his user registration script in the PHP 6 & MySQL 5 book? I think he sets a default value when a user is registered, and only changes that value once a user confirms their registration from their email account.

 

I guess the point is, I think null fields return NULL from MySQL database queries, in which case, checking the PHP.net manual should net you a way of testing for NULL, but the "easier" solution would be to set a default value, (which isn't NULL and) which signifies that a gallery has not been created yet, and just test for that value. Naturally, you'd want the value to be something that could never be set for a gallery, which is simple enough.

 

Also, Jonathon, congrats on post #200.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks HartleySan,

 

I knew it was coming soon. But I've only been able to access the forum recently on my phone as I'm relocating. I believe your #200 post is up soon too ;)

 

And yes, you are correct he uses a something like md5 encrypted random unique id in the column and then uses it in his activation script to set that column to Null essentially confirming that the user does exist.

 

:)

  • Upvote 1

Share this post


Link to post
Share on other sites

Doesn't Larry do something like this for his user registration script in the PHP 6 & MySQL 5 book? I think he sets a default value when a user is registered, and only changes that value once a user confirms their registration from their email account.

 

Thanks HartleySan, but that is completely an "apples" and "oranges" comparison! In the case of the user registration system, Larry is only doing a SELECT query on one table, and he is checking for the presence of a hashed value to see if someone had confirmed their registration through an email or not!

 

What I'm doing is very different! I should have explained it clearer above (it is a little complicated), so I apologize for that. I am doing a LEFT OUTER JOIN on two tables. The query will always output one, and only one row (given that there are no errors in the data integrity) from the 'users' table. If the user has created a profile, and hence a gallery, then there will be a matching record in the 'profiles' table ON user_id, and thus the field in the result set for 'directory_name' will have a value. Storing a boolean, or other value, in the users table to check for the presence of a gallery breaks a cardinal rule of database design in that there is now data redundancy! The simple presence of a value in the 'directory_name' field in the 'profiles' table for a given user is the same as having a boolean value to indicate such in the 'users' table.

 

Second, and more importantly, I need the 'directory_name' value so that I can create a link to it in the side menu. As a side note, running two queries is not an option! It is extremely bad programming practice to do so, especially in such a simple case as a user login! It would be akin to washing your car, going in and taking a shower, and then coming back out to wash the tires and polish the rims!

 

I suppose I could do the test for NULL within the query itself as you mentioned, but I also need to check for an empty string as well (in the rare event that a record in the profile table has been created, but the 'directory_name' field is somehow empty.

 

the "easier" solution would be to set a default value, (which isn't NULL and) which signifies that a gallery has not been created yet, and just test for that value. Naturally, you'd want the value to be something that could never be set for a gallery, which is simple enough.

 

This is a pretty good idea! Thanks for that!

 

Jonathon, congrats as well for the #200 post!

Share this post


Link to post
Share on other sites

Ah, I see what you are saying! I thought you were insisting that I create a boolean field in the 'users' table to indicate whether a gallery had been created.

 

My apologies for that!

 

I will look into your suggestions!

Share this post


Link to post
Share on other sites

I guess in the end, if you want to test for both NULL and an empty string (which seems unnecessary), just do the following:

 

if (($row['something'] === NULL) || ($row['something'] === ''))

  • Upvote 1

Share this post


Link to post
Share on other sites

I guess in the end, if you want to test for both NULL and an empty string (which seems unnecessary), just do the following:

Jon, I agree. However, if you get anything out of Larry's books, then the thing that is glaringly obvious is that nothing can be overlooked when dealing with data!

Always assume the worst and then work backwards! That's what separates half ass web programming from good!

 

if (($row['something'] === NULL) || ($row['something'] === ''))

 

Thank you! I will test this.

Share this post


Link to post
Share on other sites

if (($row['something'] === NULL) || ($row['something'] === ''))

 

Apparently empty() does the same thing as the above!

 

I found this code in the comments on php.net:

<?php
if (isset($var) && !empty($var)) {
 echo $var;
} else {
 echo "missing variable '$var'";
}
?>

 

The only problem is that it considers 0, and "0" to be empty values and returns TRUE!

  • Upvote 1

Share this post


Link to post
Share on other sites

Well then, use === NULL or == NULL. There's no confusion with 0 there.

 

Also, because there is another user on the board named Jonathon, you probably shouldn't call me by my first name.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...