Jump to content

Login Redirection


mikebyrne

Recommended Posts

I want to setup some login redirection on my page. My database is setup so that employees are assigned "2" on registration and customers "1". The is is stored in the field usertype.

 

If an Employee logs on I want them to be redirected to ../Admin_files/start.php and the customers to be redirected to ../Main Page/first

 

At present my code looks like this

 

<?php
include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
session_start();
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1)
   {
while($row = mysql_fetch_assoc($result)) {
      // Register $myusername, $mypassword and redirect to file "login_success.php"
      $_SESSION['uname'] = $row['username'];
      $_SESSION['pword'] = $row['password'];
      //The : acts as a Else.  So if row user =1 then its a Cust, otherwise it's a Emp.
      $_SESSION['type'] = ($row['User']==1)?"Cust":"Emp";
    

header("Location: ../admin_files/start.php.php");
}
   }else{
      echo "Wrong Username or Password";
   }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>

 

Link to comment
Share on other sites

I would recommend using mysql_result rather than a while { }.

 

Try:

<?php

if ($_SESSION['type'] == "Emp")
       {
              header("Location: ../admin_files/start.php");
       }
else if ($_SESSION['type'] == "Cust")
       {
              header("Location: ../Main Page/first.php");
       }

?>

Link to comment
Share on other sites

Ok my code looks like this now

 


    <?php
include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
session_start();
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if ($_SESSION['type'] == "Emp")
       {
              header("Location: ../admin_files/start.php");
       }
else if ($_SESSION['type'] == "Cust")
       {
              header("Location: ../Main Page/first.php");
       }
?>

 

I'm a little confused as to how the code tells what type of user it as as my "user" field only has two values 1 or 2 to tell the usertype 

Link to comment
Share on other sites

Would that be

 

if ($_SESSION['type'] == "2")

      {

              header("Location: ../admin_files/start.php");

      }

else if ($_SESSION['type'] == "1")

      {

              header("Location: ../Main Page/first.php");

      }

 

or

 

if ($_SESSION['usertype'] == "2")

      {

              header("Location: ../admin_files/start.php");

      }

else if ($_SESSION['usertype'] == "1")

      {

              header("Location: ../Main Page/first.php");

      }

Link to comment
Share on other sites

Whichever you want the [' '] inside the session is just the name of a variable in the session

 

on start.php you would go something like

 

<?

if (isset($_SESSION['type']) && $_SESSION['type'] == 1) {
   echo "You're logged in";
} else {
   echo "You must be logged in";
}
?>

 

and same with first.php just change the 1 to 2

Link to comment
Share on other sites

Im getting the error

 

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\Main Page\login.php:5) in C:\xampp\htdocs\Main Page\login.php on line 10

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\Main Page\login.php:5) in C:\xampp\htdocs\Main Page\login.php on line 10

 

Line 10 refers to:

 

<?php session_start();?>

 

My code up to line 10  looks like

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CD WOW!   - CD - DVD - GAMES | Best Prices & FREE Delivery Worldwide!</title><meta name="description" content="CD WOW! CDs, DVDs & Games at unbeatable prices with FREE delivery worldwide. www.cdwow.ie" /><meta name="keywords" content="cd's, cds, cheapest, music, cd, dvd, games, buy, free delivery, ireland, shop, bargain, low price, low, price, cheapest, cheap, gift" />

<link rel="stylesheet" type="text/css" href="../Main Page/style_main.css">
<link rel="stylesheet" type="text/css" href="../Main Page/style_1.css" title="stylesheet">

<?php session_start();?>

 

Link to comment
Share on other sites

<?php
include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
session_start();
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if ($_SESSION['usertype'] == "2")
       {
              header("Location: ../admin_files/start.php");
       }
else if ($_SESSION['usertype'] == "1")
       {
              header("Location: ../Main Page/first.php");
       }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top"> 
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>  

 

Link to comment
Share on other sites

How are you verifying what their usertype is? Is it in the database? If so here is what you should be doing. Everything I've added will have a /* comment above and below it

 

 

<?php

<?php
/* This always goes at the top */
session_start();

include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);
        
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this  */
        /* After verifying, we will then set the session, this is assuming that there is a column  */
        /* in your table named 'type' and its value is either '1' or '2'                                     */
        if ($count > 0) {
               $_SESSION['type'] = $r['type'];
               $logged = true;
        } else {
               echo "Wrong Username/Password.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged = 'true')
       {
              header("Location: ../admin_files/start.php");
       }
else if ($_SESSION['usertype'] == "1" && logged = 'true')
       {
              header("Location: ../Main Page/first.php");
       }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top"> 
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>  

?>

 

 

There's PLENTY of other ways to do it. That's just the basic idea of how it works, you can edit it and move stuff around. For your start.php and first.php pages would go something along the lines of what I said in a post above.

 

<?php
if (isset($_SESSION['type']) && $_SESSION['type'] == 1) {
   echo "You're logged in";
} else {
   echo "You must be logged in";
}
?>

 

 

You will also want to add some security to this. This is just to help you get it working.

if (isset[$_SESSION

Link to comment
Share on other sites

Im getting the error

 

 

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\Main Page\login.php on line 303

 

line 303 is:

 

else if ($_SESSION['usertype'] == "1" && logged = 'true')

 

my php so far

 

    <?php
session_start();

include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);
        
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this  */
        /* After verifying, we will then set the session, this is assuming that there is a column  */
        /* in your table named 'type' and its value is either '1' or '2'                                     */
        if ($count > 0) {
               $_SESSION['type'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Password.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged = 'true')
       {
              header("Location: ../Admin_files/start.php");
       }
else if ($_SESSION['usertype'] == "1" && logged = 'true')
       {
              header("Location: ../Main Page/first.php");
       }
}
?>

Link to comment
Share on other sites

No my page still doesnt go anywhere, just refreshes!!!

 

<?php
session_start();

include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);
        
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this  */
        /* After verifying, we will then set the session, this is assuming that there is a column  */
        /* in your table named 'type' and its value is either '1' or '2'                                     */
        if ($count > 0) {
               $_SESSION['type'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Pad =ssword.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged == "true") 
       {
              header("Location: ../Admin_files/start.php");
       }

else if ($_SESSION['usertype'] == "1" && $logged == "true") 
       {
           
	     header("Location: ../Main Page/first.php");
       }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top"> 
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>  

 

Link to comment
Share on other sites

No my page still doesnt go anywhere, just refreshes!!!

 

<?php

        if ($count > 0) {
               $_SESSION['type'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Pad =ssword.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged == "true") 
       {
              header("Location: ../Admin_files/start.php");
       }

else if ($_SESSION['usertype'] == "1" && $logged == "true") 
       {
           
	     header("Location: ../Main Page/first.php");
       }
}
?>

 

 

 

Ah blah, change this:

 

<?php $_SESSION['type'] = $r['usertype']; ?>

 

 

to this:

 

<?php $_SESSION['usertype'] = $r['usertype']; ?>

 

 

If you noticed I gave you code that was checking for a $_SESSION named 'usertype' when it was being set as 'user'

 

 

 

Link to comment
Share on other sites

Getting the error

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Main Page\login.php:5) in C:\xampp\htdocs\Main Page\login.php on line 306

 

Line 306 is :  header("Location: ../Main Page/first.php")

 

<?php

include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);
        
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this  */
        /* After verifying, we will then set the session, this is assuming that there is a column  */
        /* in your table named 'type' and its value is either '1' or '2'                                     */
        if ($count > 0) {
               $_SESSION['usertype'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Pad =ssword.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged == "true") 
       {
              header("Location: ../Admin_files/start.php");
       }

else if ($_SESSION['usertype'] == "1" && $logged == "true") 
       {
           
	     header("Location: ../Main Page/first.php");
       }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top"> 
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>  

 

 

Link to comment
Share on other sites

Ive changed the name to MainPage as suggested but im still getting the same problem with the page just refreshing

 

 

The top of the page login.php looks like this

 

<?php session_start();?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

 

the php looks like this now

 

<?php
include("adminconnect.php");

$tbl_name="adminusers";

if(isset($_POST['uname']) && isset($_POST['pword']))
{
// Define $myusername and $mypassword
$uname=mysql_escape_string($_POST['uname']);
$pword=mysql_escape_string($_POST['pword']);


$sql="SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);
        
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this  */
        /* After verifying, we will then set the session, this is assuming that there is a column  */
        /* in your table named 'type' and its value is either '1' or '2'                                     */
        if ($count > 0) {
               $_SESSION['usertype'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Pad =ssword.";
        }
            
if ($_SESSION['usertype'] == "2" && $logged == "true") 
       {
              header("Location: ../Admin_files/start.php");
       }

else if ($_SESSION['usertype'] == "1" && $logged == "true") 
       {
           
	     header("Location: ../MainPage/first.php");
       }
}
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%"> 
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr> 
    <td class="bgrl"> 
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top"> 
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>  

 

Link to comment
Share on other sites

Ahh.. I think I see why it's just 'refreshing' now. Try this:

 

<?php
include("adminconnect.php");

$tbl_name = "adminusers";

if(isset($_POST['uname']) && isset($_POST['pword'])) {
// Define $myusername and $mypassword
$uname  = mysql_escape_string($_POST['uname']);
$pword  = mysql_escape_string($_POST['pword']);


$sql    = "SELECT * FROM $tbl_name WHERE username='$uname' and password='$pword'";
$result = mysql_query($sql);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

        /* grab the usertype */
        $r = mysql_fetch_array($result);

        /****
        /* We will verify that the num_rows exists, for some reason you weren'y verifying this
        /* After verifying, we will then set the session, this is assuming that there is a column
        /* in your table named 'type' and its value is either '1' or '2'
        /****/
        if ($count > 0) {
               $_SESSION['usertype'] = $r['usertype'];
               $logged = true;
        } else {
               echo "Wrong Username/Pad =ssword.";
        }

        if ($_SESSION['usertype'] == "2" && $logged == "true") {
            header("Location: ../Admin_files/start.php");
        } elseif ($_SESSION['usertype'] == "1" && $logged == "true") {
            header("Location: ../MainPage/first.php");
        }

/* The form has not yet been submitted. */
} else {
?>
      <form action="login.php" method="POST">
      <div align="right">Pleas enter your Login Name</b>

<BR>(This is the name you signed up with)</div>
    </td>
    <td width="50%">
      <input type="text" name="uname" size="12" maxlength="50" class="field">
    </td>
  </tr>
  <tr>
    <td class="bgrl">
      <div align="right">Please enter your <b>password</b></div>

    </td>
    <td width="50%" class="bgrl">
      <input type="password" name="pword" size="18" class="field">
    </td>
  </tr>
  <tr><td> </td>
    <td>
    <input type="submit" value="Login »" class="submit"></form></td>
  </tr>



  <tr><td colspan="2" class="genericside"><span class="t11bw">Forgotten your password?</span></td>
  </tr>
  <tr valign="top">
    <td class="alignright">
    <form name="passwordreminder" action="/member_login.php" method="post">
    <input type="hidden" name="type" value="passwordreminder">
    Please enter your email here:</td>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>

<td><input type="text" name="reminderemail" class="field" maxlength="50"><br>
<input type="submit" value="Send Password »" class="submit">
    </form>
<?php
}
?>

Link to comment
Share on other sites

No that still brings up the error

 

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\MainPage\login.php:5) in C:\xampp\htdocs\MainPage\login.php on line 301

 

Plus my form doesnt appear

 

again line303 is: header("Location: ../MainPage/first.php");

 

The start of first.php

 

<?PHP

if (isset($_SESSION['usertype']) && $_SESSION['usertype'] == 1) {
   echo "You're logged in";
} else {
   echo "You must be logged in";
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.