ex: If user=1 send to user1.php page or
if user=2 send to user2.php. else
redirect to user page login page.
lionaneesh -4 Junior Poster
A simple script to do that :-
<?php
if(isset($_SESSION['user']))
{
$user = $_SESSION['user'];
header("Location:http://www.localhost.com/$user.php");
}
else
{
header("Location:http://www.localhost.com/login.php");
}
?>
What this does is that it searches for a defines $_SEESION for the username if the session is defined it redirects the page to :-
http://www.localhost.com/username.php
else it redirects the page to:-
http://www.localhost.com/login.php
cristi08 0 Junior Poster
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?
Zagga
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
Zagga
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 because: n/a
lionaneesh -4 Junior Poster
@cristi Are you talking about SQL tables....
cristi08 0 Junior Poster
Wait a minut zagga,let me think a little bit. I am very blury
cristi08 0 Junior Poster
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?
lionaneesh -4 Junior Poster
Cant Get you....
Zagga
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?
lionaneesh -4 Junior Poster
Yes
cristi08 0 Junior Poster
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">
<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">
<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">
<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 because: Fixed formatting
Zagga
In your database, is there a field that says if the user is type 1 or type 2?
Edited by Zagga because: n/a
cristi08 0 Junior Poster
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">
<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">
<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">
<input type="reset" value="Clear">
</form>
</p>
<?php
}
?>
</body>
</html>
cristi08 0 Junior Poster
yes it is
Zagga
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 because: n/a
cristi08 0 Junior Poster
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.
Zagga
You are welcome :)
I'm glad I could help.
Zagga
cristi08 0 Junior Poster
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();
}
Zagga
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?
cristi08 0 Junior Poster
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>
cristi08 0 Junior Poster
You remain the gratest....you are again wright...
Zagga
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?
cristi08 0 Junior Poster
Do you know a good book about php and mysql i have a thew but i did not find what i need about session
Zagga
http://www.w3schools.com/php/php_sessions.asp
is a good place to start. w3schools has some very usefull information about all the php features. With that and php.net you should get most of what you need (along with DaniWeb of course)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.