0

ex: If user=1 send to user1.php page or
if user=2 send to user2.php. else
redirect to user page login page.

3
Contributors
24
Replies
27
Views
7 Years
Discussion Span
Last Post by Zagga
0

THANK YOU FOR YOUR REPLY

For sure it is wright but how to set a session that retrieve the username from user tabel? And execute the code that you replyed?

0

Hi cristi08,

In addition to the suggestion from lionaneesh, you could add the following code to the very top of each of the user pages.

<?php
session_start();
if (!isset($_SESSION['user'])){
	header("location:login.php");
	exit();
}

This will prevent anyone from accessing your members pages by surfing directly to the address of the page without going through your login system (it will redirect them to your login page).


Hope this helps.
Zagga

0

Setting the session is fairly simple.
On the page that checks the user login, after they have actually logged in, add the following code.

session_start();
$_SESSION['user'] = $username;

I am assuming you have set whatever the username is to the variable $username.

Zagga

Edited by Zagga: n/a

0

Wait a minut zagga,let me think a little bit. I am very blury

0

the variable $username is not set it just sent from the login form to mysql database in table user_info.
When the user is redirect to his page this should be a diferent page for each type of user. Are only two type 1 and 2. How to fetch variable and redirect diferent for each one?

0

Ok, lets see if I understand this.

You have a login page that takes the username and password that is typed into the form.
To get the username from the form you must have used something like $username = $_POST['username'] Is that right so far?

0

Manny apologize but I don’t know nothing about $_SESSION.
I have this on index.php

session_start();
//verifica existenta parolei si utilizatorului
if ((isset($_SESSION['user_logged']) &&
$_SESSION['user_logged'] != "") ||
(isset($_SESSION['user_password']) &&
 $_SESSION['user_password'] != "")) {
//timite utilizatorul in pagina logged user daca acesta este logat
include "logged_user.php";
} else {
//trimite utilizatorul in pagina unlogged daca aceste nu este logat
include "unlogged_user.php";
}
?>

AND ON REGISTER.PHP THIS

?>
<html>
<head>
<title>Beginning PHP5, Apache and MySQL</title>
</head>
<body>
<?php
if (isset($_POST['submit']) && $_POST['submit'] == "Register") {
if ($_POST['username'] != "" &&
$_POST['password'] != "" &&
$_POST['email' ]!= "" &&
$_POST['Nume'] != "" &&
$_POST['option']!= "" &&
$_POST['utilizator']!= "" &&
$_POST['Prenume'] != "") {

$query = "SELECT username
            FROM user_info 
            WHERE username = '" . $_POST['username'] . "'";
    $result = mysql_query($query)
        or die(mysql_error());
        if (mysql_num_rows($result) != 0) {
?>
<p>
<font color="#FF0000"><b>The Username,
<?php echo $_POST['username']; ?>, is already in use, please choose
another!</b></font>
<form action="register.php" method="post">
Username:<p><input type="text" name="username"></p>
Password:<p><input type="password" name="password"
value="<?php echo $_POST['password']; ?>"></p>
Email:<p><input type="text" name="email"
value="<?php echo $_POST['email']; ?>"></p>
Nume:<p><input type="text" name="Nume"
value="<?php echo $_POST['Nume']; ?>"></p>
Prenume:<p><input type="text" name="Prenume"
value="<?php echo $_POST['Prenume']; ?>"></p>
         Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>   
<p>Alegeti orasul dumneavoastra:
<p><?php

          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}

    ?>

             <select name="option" >

<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}

?>
</p>
<input type="submit" name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
} else {
$query = "INSERT INTO user_info
        (username,
        password,
        email,
        Nume,
        Prenume,
        Oras,
        Utilizator
        ) 
            VALUES
        ('". $_POST['username'] ."',
        '" . $_POST['password'] . "',
        '" . $_POST['email'] . "',
        '" . $_POST['Nume'] ."',
        '" . $_POST['Prenume'] . "',
        '" . $_POST['option'] ."',
        '" . $_POST['utilizator'] ."'
        )";
$result = mysql_query($query)
or die(mysql_error());
?>
<?php
$query1="INSERT tblclient (Oras,Nume_client)
        SELECT Oras,Nume
        FROM user_info
        WHERE Utilizator=2";

    if (isset($query1) && !empty($query1)) {  
      echo"<!--" . $query1 . "-->";
          $result1 = mysql_query($query1)
       or die ("invalid query 4: " . mysql_error ());}

$query2="INSERT ofertant (Oras,Nume)
        SELECT Oras,Nume
        FROM user_info
        WHERE Utilizator=1";

    if (isset($query2) && !empty($query2)) {  
      echo"<!--" . $query2 . "-->";
          $result = mysql_query($query2)
       or die ("invalid query 4: " . mysql_error ());}



$_SESSION['user_logged'] = $_POST['username'];
$_SESSION['user_password'] = $_POST['password'];
?>
<p>
Thank you, <?php echo $_POST['Nume'] ." " .
$_POST['Prenume']; ?> for registering!<br>
<?php
header("Refresh: 10; URL=index.php");
echo "Your registration is complete! " .
"You are being sent to the page you requested!<br>";
echo "(If your browser doesn’t support this, " .
"<a href=\"index.php\">click here</a>)";
die();
}
} else {
?>
<p>
<font color=”#FF0000”><b>The Username, Password, Email, First Name,
and Last Name fields are required!</b></font>
<form action="register.php" method="post">
Username: <input type="text" name="username"
value="<?php echo $_POST['username']; ?>"><br>
Password: <input type="password" name="password"
value="<?php echo $_POST['password']; ?>"><br>
Email: <input type="text" name="email"
value="<?php echo $_POST['email']; ?>"><br>
First Name: <input type="text" name="Nume"
value="<?php echo $_POST['Nume']; ?>"><br>
Last Name: <input type="text" name="Prenume"
value="<?php echo $_POST['Prenume']; ?>"><br>
     Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>   
<p>Alegeti orasul dumneavoastra:
<p><?php

          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}

    ?>
             <select name="option" >

<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}
?>
</p>
<input type="submit" name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
}
} else {
?>
<p>
Welcome to the registration page!<br>
The Username, Password, Email, First Name, and Last Name fields
are required!
<form action="register.php" method="post">
Username: <p><input type="text" name="username"></p>
Password: <p><input type="password" name="password"></p>
Email:<p><input type="text" name="email"></p>
First Name:<p> <input type="text" name="Nume"></p>
Nume:<p> <input type="text"name="Prenume"></p>
    Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>      
<p>Alegeti orasul dumneavoastra:
<p><?php

          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}

    ?>
             <select name="option" >

<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}
?>
</p>User Logins, Profiles, and Personalization

</select><br><br>
<input type="submit"name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
}
?>
</body>
</html>

"Utilizator" is the option that I need to send the user on diferent page how?

Edited by mike_2000_17: Fixed formatting

0

In your database, is there a field that says if the user is type 1 or type 2?

Edited by Zagga: n/a

0

SORRY AND register.php contain this code

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
session_start();
$delete="DELETE FROM
        calculatii.ofertant";
        $result = mysql_query($delete);

$delete="DELETE FROM
        calculatii.tblclient";
        $result = mysql_query($delete);
?>
<html>
<head>
<title>Beginning PHP5, Apache and MySQL</title>
</head>
<body>
<?php
if (isset($_POST['submit']) && $_POST['submit'] == "Register") {
if ($_POST['username'] != "" &&
$_POST['password'] != "" &&
$_POST['email' ]!= "" &&
$_POST['Nume'] != "" &&
$_POST['option']!= "" &&
$_POST['utilizator']!= "" &&
$_POST['Prenume'] != "") {

$query = "SELECT username
            FROM user_info 
            WHERE username = '" . $_POST['username'] . "'";
    $result = mysql_query($query)
        or die(mysql_error());
        if (mysql_num_rows($result) != 0) {
?>
<p>
<font color="#FF0000"><b>The Username,
<?php echo $_POST['username']; ?>, is already in use, please choose
another!</b></font>
<form action="register.php" method="post">
Username:<p><input type="text" name="username"></p>
Password:<p><input type="password" name="password"
value="<?php echo $_POST['password']; ?>"></p>
Email:<p><input type="text" name="email"
value="<?php echo $_POST['email']; ?>"></p>
Nume:<p><input type="text" name="Nume"
value="<?php echo $_POST['Nume']; ?>"></p>
Prenume:<p><input type="text" name="Prenume"
value="<?php echo $_POST['Prenume']; ?>"></p>
         Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>   
<p>Alegeti orasul dumneavoastra:
<p><?php
    
          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}
    
    ?>
            
             <select name="option" >
            
<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}
       
?>
</p>
<input type="submit" name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
} else {
$query = "INSERT INTO user_info
        (username,
        password,
        email,
        Nume,
        Prenume,
        Oras,
        Utilizator
        ) 
            VALUES
        ('". $_POST['username'] ."',
        '" . $_POST['password'] . "',
        '" . $_POST['email'] . "',
        '" . $_POST['Nume'] ."',
        '" . $_POST['Prenume'] . "',
        '" . $_POST['option'] ."',
        '" . $_POST['utilizator'] ."'
        )";
$result = mysql_query($query)
or die(mysql_error());
?>
<?php
$query1="INSERT tblclient (Oras,Nume_client)
        SELECT Oras,Nume
        FROM user_info
        WHERE Utilizator=2";
       
    if (isset($query1) && !empty($query1)) {  
      echo"<!--" . $query1 . "-->";
          $result1 = mysql_query($query1)
       or die ("invalid query 4: " . mysql_error ());}
        
$query2="INSERT ofertant (Oras,Nume)
        SELECT Oras,Nume
        FROM user_info
        WHERE Utilizator=1";
       
    if (isset($query2) && !empty($query2)) {  
      echo"<!--" . $query2 . "-->";
          $result = mysql_query($query2)
       or die ("invalid query 4: " . mysql_error ());}
        


$_SESSION['user_logged'] = $_POST['username'];
$_SESSION['user_password'] = $_POST['password'];
?>
<p>
Thank you, <?php echo $_POST['Nume'] ." " .
$_POST['Prenume']; ?> for registering!<br>
<?php
header("Refresh: 10; URL=index.php");
echo "Your registration is complete! " .
"You are being sent to the page you requested!<br>";
echo "(If your browser doesn’t support this, " .
"<a href=\"index.php\">click here</a>)";
die();
}
} else {
?>
<p>
<font color=”#FF0000”><b>The Username, Password, Email, First Name,
and Last Name fields are required!</b></font>
<form action="register.php" method="post">
Username: <input type="text" name="username"
value="<?php echo $_POST['username']; ?>"><br>
Password: <input type="password" name="password"
value="<?php echo $_POST['password']; ?>"><br>
Email: <input type="text" name="email"
value="<?php echo $_POST['email']; ?>"><br>
First Name: <input type="text" name="Nume"
value="<?php echo $_POST['Nume']; ?>"><br>
Last Name: <input type="text" name="Prenume"
value="<?php echo $_POST['Prenume']; ?>"><br>
     Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>   
<p>Alegeti orasul dumneavoastra:
<p><?php
    
          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}
    
    ?>
             <select name="option" >
    
<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}
?>
</p>
<input type="submit" name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
}
} else {
?>
<p>
Welcome to the registration page!<br>
The Username, Password, Email, First Name, and Last Name fields
are required!
<form action="register.php" method="post">
Username: <p><input type="text" name="username"></p>
Password: <p><input type="password" name="password"></p>
Email:<p><input type="text" name="email"></p>
First Name:<p> <input type="text" name="Nume"></p>
Nume:<p> <input type="text"name="Prenume"></p>
    Tip de utilizator:<p>
    <select name='utilizator'></p>
<?php     
 echo"  <option value='1'>Executant Liber Profesionist</option>
        <option value='1'>Executant Societate Comercial</option>
        <option value='2'>Propietar Persoana Fizica</option>
        <option value='2'>Propietar Persoana Juridica</option>
        </select>";3
     ?>      
<p>Alegeti orasul dumneavoastra:
<p><?php
    
          $qorase="SELECT Oras FROM oras";
    if (isset($qorase) && !empty($qorase)) {  
      echo"<!--" . $qorase . "-->";
          $result = mysql_query($qorase)
       or die ("invalid query 4: " . mysql_error ());}
    
    ?>
             <select name="option" >
    
<?php    while ($row = mysql_fetch_array($result)){
    echo "<option value=\"$row[Oras]\">$row[Oras]</option>\n";

       ;}
?>
</p>User Logins, Profiles, and Personalization

</select><br><br>
<input type="submit"name="submit" value="Register"> &nbsp;
<input type="reset" value="Clear">
</form>
</p>
<?php
}
?>
</body>
</html>
0

Ok

in register.php at lines 128 and 129 you set some session variables

$_SESSION['user_logged'] = $_POST['username'];
$_SESSION['user_password'] = $_POST['password'];

add

$_SESSION['utilizator'] = $_POST['utilizator'];

then add the following code to index.php (wherever you redirect the logged in users)

if ($_SESSION['utilizator'] == "1"){
	header("location:page1.php");
}
elseif ($_SESSION['utilizator'] == "2"){
	header("location:page2.php");
}
else {  // no utilizator set
	header("location:login.php");
	exit();
}

Edited by Zagga: n/a

0

YOU ARE THE GREATEST. Thanks a lot for your trouble. I ask my self if will come the day one I can solve this kind of problems fast as you.

0

ZAGGA ....THE PAGE IS REDIRECTING WRONG....WHERE IS THE PROBLEM?

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
session_start();
//verifica existenta parolei si utilizatorului
if ((isset($_SESSION['user_logged']) &&
$_SESSION['user_logged'] != "") ||
(isset($_SESSION['user_password']) &&
 $_SESSION['user_password'] != "")) {
//timite utilizatorul in pagina logged user daca acesta este logat
include "logged_user.php";
} else {
//trimite utilizatorul in pagina unlogged daca aceste nu este logat
include "unlogged_user.php";
}
      if ($_SESSION['utilizator'] == "1"){
      header("location:page.php");
      }  
      elseif ($_SESSION['utilizator'] == "2"){

      header("proiectnou.php");

      }

      else { // no utilizator set

      header("ofertantnou.php");

      exit();

      }
0

The header statement is in the wrong format.
They should be

header("location:proiectnou.php");

Also, with your code, if utilizator is 1, they will be redirected to page.php. Is that the correct page?

0

Here is the problem: This page is involved (logged_user.php)

<html>
<head>
<title>Beginning PHP5, Apache and MySQL</title>
</head>
<body>
<h1>Welcome to the home page!</h1>
<p>
Thank you for logging into our system
<b><?php echo $_SESSION['user_logged']; ?></b>.<br>
You may now <a href="user_personal.php">click here</a>
to go into your own personal information area, and
update or remove your information should you wish to do so.
</p>
</body>
</html>
0

I'm a little confused again :)

if utilizator is 1, the user should go to logged_user.php
if utilizator is 2, the user should go to proiectnou.php
if utilizator is not set, the user should go to ofertantnou.php
if the user got the username or password wrong they should go to unlogged_user.php

Is that right?

0

Do you know a good book about php and mysql i have a thew but i did not find what i need about session

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.