Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using some code to redirect to the page from where login is requested.
But the code is not working at all.

please help.
login.php:

XML
<?php

// Inialize session
session_start();

// Check, if user is already login, then jump to secured page
if (isset($_SESSION['username'])) {
header('Location: '.$_REQUEST['from']);

}

?>
<html>

<head>
<title>Kanu Bhawan - Member Login</title>
</head>

<body>

<h3>User Login</h3>

<table border="0">
<form method="GET" action="loginproc.php">
<tr><td>Username</td><td>:</td><td><input type="text" name="email" size="20"></td></tr>
<tr><td>Password</td><td>:</td><td><input type="password" name="password" size="20"></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr>
<input type="hidden"  name="id">
</form>
</table>

</body>

</html>


?>


loginproc.php
PHP
<?php

session_start();
require "config.php"; 
$login = mysql_query("SELECT * FROM members WHERE (email = '" . mysql_real_escape_string($_POST['email']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");

// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['email'];
// Jump to secured page
header('Location: '.$_GET['from']);

}
else {
// Jump to login page
header('Location: login.php');
}

?>


fullprofile.php

PHP
<?php

// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: login.php?from='.rawurlencode($_SERVER['REQUEST_URI'])); 
}

?>
<html>

<head>
<title>Secured Page</title>
</head>

<body>

<p>This is secured page with session: <?php echo $_SESSION['username']; ?>
<br>You can put your restricted information here.</p>
<p><a href="logout.php">Logout</a></p>

</body>

</html>

please help
Posted
Updated 21-Sep-12 11:34am
v3
Comments
Er. Tushar Srivastava 21-Sep-12 16:56pm    
are you posting the address of the url from where you have come to this (current page ie login page)... inside the form through some hidden links ?
Er. Tushar Srivastava 21-Sep-12 16:57pm    
Since if it is the case, then you must have to check what you are posting, since otherwise this code is correct...
[no name] 21-Sep-12 16:59pm    
the second lase line means
header('Location: '.$_POST['from']);
$_POST['from'] contains nothing
[no name] 21-Sep-12 16:58pm    
no hidden links
all the code is given.
please refer this.
Er. Tushar Srivastava 21-Sep-12 16:58pm    
And as I have just seen, you are actually sending the 'from' into the url, in that case the $_POST['from'] will not work at all, use $_REQUEST['from'] or $_GET['from'] :-)

 
Share this answer
 
v3
Comments
[no name] 21-Sep-12 17:11pm    
You didn't read the question
worthless links.
sorry for down vote.
Oh ok...
I am sorry for this...
I thought of the requirement and searched for you.
Can't you implement the things discussed in the links or do u have completely different requirement ?
[no name] 21-Sep-12 17:27pm    
I have completely different requirement.
[no name] 21-Sep-12 17:36pm    
now you can help me.
I post all the code i have used.
Hi Friend,
Use this :

PHP
header('Location: '.$_REQUEST['from']);


It will work as the data 'from' is not posted but is sent into the url and is known as get method so better to keep yourself on safe side, use, $_REQUEST, yet you can also use $_GET

UPDATE 2:
I have found a problem, See the code inside login.php :
PHP
<form method="get" action="loginproc.php">
Username :<input type="Text" name="username" />
Password :<input type="password" name="password" />
<input type="hidden" name="from" value=<?php echo $_REQUEST['from']; ?>>
<input type="submit" value="Login">
</form>


As you can See I have added an extra hidden field which is posting the address of redirection alongwith the form so, now the error will not occur.

Also, you need a code revision, as this code is not very well implemented and I will suggest you to implement it correctly. You can use some data flowchart kind of diagram to see how the request will flow between the pages when a login event occurs, so that a user never fall into a loop wherein the browser says, "The Browser is redirecting in a way that it will never complete" So, better have a visual look of how the request flows between all the pages, it will become very easy then to handle such situations :-)

Hope it helped,
Regards
Tushar Srivastava
 
Share this answer
 
v3
Comments
[no name] 21-Sep-12 17:13pm    
error:

The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Er. Tushar Srivastava 21-Sep-12 17:15pm    
Ok.... Let me think for a while, since there is possible error in sending the address... Can you please post a snippet of code that you have just edited..
[no name] 21-Sep-12 17:17pm    
from=/xampp/shadi/fullprofile.php?id=3
this is the quesrystring, which is sending
can we change it to only
fullprofile.php?id=3

instead of using
from='.$_SERVER['REQUEST_URI']);
Er. Tushar Srivastava 21-Sep-12 17:21pm    
That will work possibly.. by the way, are you saving this project under C://xampp/htdocs/xampp/shadi/ ?
Please tell me this :-)
[no name] 21-Sep-12 17:26pm    
yes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900