Solved

Session problem. Could be simple for you guys

Posted on 2007-11-30
10
169 Views
Last Modified: 2013-12-12
I am using the code below and checking a user authentication. I want to start the session if the username and password is matched and send him to a new page. For some reason after I hit the submit button, the page goes blank. So there is an error. Please have a look at the code below
Thanks
<?PHP
 
require_once('info.php');
 
$form = "<h4> <center><br><br>
<form action='$_SERVER[PHP_SELF]' method='post'>
UserName: <input type='text' name='user'><br><br>
Password : <input type='password' name='pass'><br>
 
<br><br>
<input type='submit' name='submit' size='10' value='Login'>
</form></center></h4>";
 
if($_POST["submit"]) {
 
        if($_POST['newpass']) {
                
                $newpass = mysql_real_escape_string($_POST['newpass']);
                $confirmpass = mysql_real_escape_string($_POST['confirmpass']);
				$user = mysql_real_escape_string($_POST['user']);
                
                if($newpass == $confirmpass) {
                        
                        $insertquery = "INSERT INTO users(user, pass, flag) VALUES ('".$user."','".$newpass ."',2)";
                        
                        $resultinsert = mysql_query($insertquery) or die(mysql_error());
                        
                        //$message = "Password inserted into database";
session_start();
$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['pass'];
header ('Location: newpage.php');
 
}
                
                }else{
                
                        $message = "Password Dont Match";
                
                }
        
        }else{
        
        
        $user = mysql_real_escape_string($_POST['user']);
 
        $pass = mysql_real_escape_string($_POST['pass']);
 
        $result = mysql_query("SELECT user,pass FROM users WHERE (pass='" . $pass. "' AND flag=1)  OR (user='". $user."' AND pass='".$pass."')") or die(mysql_error());
 
        $num = mysql_num_rows($result);
        
        if (!$num) {
        
                $message = "Username & Password Dont match <br><br>";
                $message .= $form;
        
        }else{
        
                 list($dbuser,$dbpass) = mysql_fetch_row($result);
                 
                  if($dbuser==$user && $dbpass==$pass) {
                  
                        //$message =  "You entered a username & password";
						session_start();
						$_SESSION['user'] = $_POST['user'];
						$_SESSION['pass'] = $_POST['pass'];
						header ('Location: newpage.php');
                  
                  }else {
                  
                        $message =  "<h4> <center><br><br>
                                <form action='$_SERVER[PHP_SELF]' method='post'>
                                New PassWord: <input type='text' name='newpass'><br><br>
                                Confirm Password : <input type='password' name='confirmpass'><br>
								<input type='hidden' name='user' value='".$user."'>
 
                                <br><br>
                                <input type='submit' name='submit' size='10' value='Login'>
                                </form></center></h4>";
                  
                  }
                 
                 
        
        }
        
       	
		}
       
	     echo $message;
 
}else {
 
        echo $form;
        
 
}
 ?>

Open in new window

0
Comment
Question by:syedasimmeesaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 17

Accepted Solution

by:
nplib earned 200 total points
ID: 20385133
session_start();
$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['pass'];
header ('Location: newpage.php');
exit;

see if that works
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 200 total points
ID: 20385144
put
error_reporting(E_ALL);
ini_set("display_errors","On");

on the first line of your page.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20385241
after I added the code it says

Notice: Undefined index: newpass in C:\inetpub\wwwrootI\index.php on line 75

thanks
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 20

Expert Comment

by:steelseth12
ID: 20385269
thats the only notice you get ?
No warning headers already sent in ... error ??
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20385286
Lets take the notices out of the equation ...

change error_reporting(E_ALL);

to
error_reporting(E_ALL ^ E_NOTICE);
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20385332
ok the exit; worked with a little limitation
if the user has the password and login already in the database which is this part
else{
       
                 list($dbuser,$dbpass) = mysql_fetch_row($result);
                 
                  if($dbuser==$user && $dbpass==$pass) {
                 
                        //$message =  "You entered a username & password";
                                                session_start();
                                                $_SESSION['user'] = $_POST['user'];
                                                $_SESSION['pass'] = $_POST['pass'];
                                                header ('Location: newpage.php');
                                               exit;
                 
                  }
it works but if the user is asked to make a new password and confirm it  which is this part

if($_POST['newpass']) {
               
                $newpass = mysql_real_escape_string($_POST['newpass']);
                $confirmpass = mysql_real_escape_string($_POST['confirmpass']);
                                $user = mysql_real_escape_string($_POST['user']);
               
                if($newpass == $confirmpass) {
                       
                        $insertquery = "INSERT INTO users(user, pass, flag) VALUES ('".$user."','".$newpass ."',2)";
                       
                        $resultinsert = mysql_query($insertquery) or die(mysql_error());
                       
                        //$message = "Password inserted into database";
session_start();
$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['pass'];
header ('Location: newpage.php');
 exit;
}
then it doesn't work

Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20385335
yea I din't get headers already sent warning
thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20385352
so I change the above to this
session_start();
                                    $_SESSION['user'] = $_POST['user'];
                                    $_SESSION['pass'] = $_POST['newpass'];
                                    header ('Location: newpage.php');
                                    exit;

now it works but I am not sure if it is the right way to do it or it can cause any problems later on
Thanks
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 200 total points
ID: 20385388
Cant understand why the one would work and the other wouldn't .... there is some white space in front of the first but if it affected it it should give out a warning ....

Anyways you should always put session_start() at the very begining of your page .. and include it on all pages you want to transfer the session.

0
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 100 total points
ID: 20385401
syedasimmeesag,

Always put:
session_start();

as the very first line of your script.

Also, it is good practice after you have assigned your variables, to close your session, something like this:

$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['newpass'];
session_write_close();
header ('Location: newpage.php');
exit;
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question