Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

login and session questions

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

login and session questions

Postby tiger » Mon Jul 11, 2011 2:25 pm

Hello everyone,

I have some questions about these codes. I've been trying to get them to work, but I could not.

First of all, the input tag, if the user inputs a wrong input, I want to keep the info they input and show an error. I tried this, but it did not work
Code: Select all
<label>Username</label>
        <input type="text" name="username" maxlength="20" size="25" value="<?php echo $_POST['username']; ?> " />


Second, when the user clicks "Log Out", I want the session to end and the user should be directed to the index.php page. I wrote this code, but it does not take the user anywhere, it gives a blank page.
Code: Select all
<?php
require_once 'session.php';

if (isset($submit)) {
  session_destroy();
  header('Location: index.php');
  exit;
}?>




Your help is really appreciated.

Thanks in advance. :help:
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: login and session questions

Postby keith » Mon Jul 11, 2011 10:08 pm

perhaps use a constant as index.php is at root level and they could be loggin out of any location - ie from subfolder or so-on. I am no expert but understand the concept.
try using http://www.yourdomain.com instead of index.php

hope this helps
keith
New php-forum User
New php-forum User
 
Posts: 17
Joined: Fri Jul 08, 2011 2:51 am

Re: login and session questions

Postby egami » Tue Jul 12, 2011 4:32 am

The first thing, is incomplete. I need to see the entire form as you have it, in order to see what you're trying to accomplish. I need to have the name of the script, and the entire <form></form> tags.

Secondly,
if (isset($submit)) {

This simply does not exist. Every time it happens, it will bypass your script below it, because $submit, does not exist. You might try changing this to $_POST['submit'] or $_POST['Submit'] (they are different) and then see if that works for you, but based purely off of that, it's a bad idea, because many forms will have $_POST['Submit'] and you certainly don't want the user to log out before you process anything do you?

Change the button to "Logout" and then change your script to if (isset($_POST['Logout'])) and see if that works better for you.

-B
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: login and session questions

Postby egami » Tue Jul 12, 2011 4:33 am

just to add one more thing.

whenever you have an IF statement, it's a good idea to set a trap! Especially for development.

ie.

if (isset($submit)) {
do this stuff here..
} ELSE {
do this to show me that it doesn't exist
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: login and session questions

Postby tiger » Sat Jul 16, 2011 5:36 pm

Thanks a lot guys!!

I got this to work two days ago! I wanted to show what I did here just in case anyone here has faced/will face the same problem. So, what I did for the first problem was that I added the SESSION instead of POST, and this is the code

Code: Select all
<input type="text" name="username" maxlength="20" size="25" value="<?php echo $_SESSION['postedUsername'];
unset($_SESSION['postedUsername']);
?>" />



for the second problem, as egami mentioned, I needed to specify the submit button, so I had to use:

Code: Select all
<?php
if ($_POST['submit']){
do something
}
?>


Thanks again for the help guys!
Last edited by tiger on Sat Jul 16, 2011 5:42 pm, edited 1 time in total.
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: login and session questions

Postby tiger » Sat Jul 16, 2011 5:41 pm

egami wrote:just to add one more thing.

whenever you have an IF statement, it's a good idea to set a trap! Especially for development.

ie.

if (isset($submit)) {
do this stuff here..
} ELSE {
do this to show me that it doesn't exist
}


That's a good technic to keep in mind, egami! Thanks a lot man!
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm


Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron