Jump to content

Redirect to different page after successful login


mds1256

Recommended Posts

Looking to redirect my user to a new page after successfully logging in via a form.

 

I have refactored my code slightly to cut down on unnecessary code so the I was doing it no longer works (getting the headers already sent error), I know why this is happening but I need some ideas on how I can now achieve this?

 

Any ideas would be great.

 

 


function loginForm()
{
$displayForm = true;
$error = "";
$username = "";

if(isset($_POST['signInSubmit']))
{
	$username = mysqli_real_escape_string(cf_dbConnect(), $_POST['username']);
	$password = mysqli_real_escape_string(cf_dbConnect(), $_POST['password']);

	$query = mysqli_query(cf_dbConnect(), "call sp_checkLoginDetails('{$username}', '{$password}')");
	if(mysqli_num_rows($query) == 1)
	{
		$_SESSION['isLoggedIn'] = true;
		while($row = mysqli_fetch_object($query))
		{
			$_SESSION['firstName'] = $row->firstName;
		}
		mysqli_query(cf_dbConnect(), "call sp_updateLoginDateTime('{$_SESSION['firstName']}')");
		header("Location: ."); // this is the point where the headers are already sent I know why this happens, need an alternative way 
	}
	else
	{
		$error = "<div id=\"formMessages\">Username or Password is incorrect. Please try again.</div>";

	}

}

if($displayForm)
{
	echo "
		<form name=\"signInForm\" id=\"signInForm\" method=\"post\" action=\"\">
				<label for=\"username\">Username:</label><br />
				<input type=\"text\" name=\"username\" id=\"signInFormUsername\" class=\"formField\" value=\"{$username}\" /><br /><br />
				<label for=\"password\">Password:</label><br />
				<input type=\"password\" name=\"password\" id=\"signInFormPassword\" class=\"formField\" /><br /><br />
				<input type=\"submit\" id=\"signInSubmit\" name=\"signInSubmit\" value=\"Login\" />
		</form>
		<br />
		{$error}
	";
}
}


Link to comment
Share on other sites

Is this someone else's code?

 

Play with it some,


$displayForm = true;
$error = "";
$username = "";

if(isset($_POST['signInSubmit']))
{
	$username = mysqli_real_escape_string(cf_dbConnect(), $_POST['username']);
	$password = mysqli_real_escape_string(cf_dbConnect(), $_POST['password']);

	$query = mysqli_query(cf_dbConnect(), "call sp_checkLoginDetails('{$username}', '{$password}')");
	if(mysqli_num_rows($query) == 1)
	{
		$_SESSION['isLoggedIn'] = true;
		while($row = mysqli_fetch_object($query))
		{
			$_SESSION['firstName'] = $row->firstName;
		}
		mysqli_query(cf_dbConnect(), "call sp_updateLoginDateTime('{$_SESSION['firstName']}')");
		header("Location: ."); // this is the point where the headers are already sent I know why this happens, need an alternative way 
	}
	else
	{
		$error = "<div id=\"formMessages\">Username or Password is incorrect. Please try again.</div>";

	}

}

if($displayForm)
{

require_once('header.php'); 
	echo "
		<form name=\"signInForm\" id=\"signInForm\" method=\"post\" action=\"\">
				<label for=\"username\">Username:</label><br />
				<input type=\"text\" name=\"username\" id=\"signInFormUsername\" class=\"formField\" value=\"{$username}\" /><br /><br />
				<label for=\"password\">Password:</label><br />
				<input type=\"password\" name=\"password\" id=\"signInFormPassword\" class=\"formField\" /><br /><br />
				<input type=\"submit\" id=\"signInSubmit\" name=\"signInSubmit\" value=\"Login\" />
		</form>
		<br />
		{$error}
	";
}

Link to comment
Share on other sites

Have you included cf_dbConnect()  in the script ?

 

Yeah.

 

My whole code is as follows:

 

 

signin.php

 

<?php
require_once('_coreFunctions.php'); // this is where DB functions are e.g. connection etc
require_once('_signInFunctions.php');
require_once('header.php'); // this has HTML inside of it e.g. head tag etc

?>
<div id="contentBoxWrapper">
<div id="contentBoxTop">
    	<div id="contentBoxTopInner"><a href="/" class="firstLink">Home</a> <span class="breadcrumbDivider">></span> Sign In<hr /></div>
    </div>
    <div id="contentBoxContent">
                <div id="signInFormWrapper">
                <h1>Sign In</h1>
                <?php sif_loginForm(); ?> // this is where I call the login form function......
                <br />
                <a href="resetpassword">Forgotten password?</a>
                </div>
                <div id="signInRegisterFormWrapper">
                <h1>Register Now</h1>
                <form name="registerForm" method="post" action="">
                <label for="username">Username:</label><br /><input type="text" name="username" class="formField" /><br /><br />
                <label for="password">Password:</label><br /><input type="password" name="password" class="formField" /><br /><br />
                <input type="submit" value="Register" />
                </form>
                </div>
            </div>
    <div id="contentBoxBottom"></div>
</div>
<?php require_once('footer.php'); ?>

 

 

 

 

_signInFunctions.php

 

<?php
require_once('_coreFunctions.php');

function sif_loginForm()
{
$displayForm = true;
$error = "";
$username = "";

if(isset($_POST['signInSubmit']))
{
	$username = mysqli_real_escape_string(cf_dbConnect(), $_POST['username']);
	$password = mysqli_real_escape_string(cf_dbConnect(), $_POST['password']);

	$query = mysqli_query(cf_dbConnect(), "call sp_checkLoginDetails('{$username}', '{$password}')");
	if(mysqli_num_rows($query) == 1)
	{
		$_SESSION['isLoggedIn'] = true;
		while($row = mysqli_fetch_object($query))
		{
			$_SESSION['firstName'] = $row->firstName;
		}
		mysqli_query(cf_dbConnect(), "call sp_updateLoginDateTime('{$_SESSION['firstName']}')");
		header("Location: ."); // this is my redirect part
	}
	else
	{
		$error = "<div id=\"formMessages\">Username or Password is incorrect. Please try again.</div>";

		if($username == "username...")
		{
			$username = "";
		}
	}

}

if($displayForm)
{
	echo "
		<form name=\"signInForm\" id=\"signInForm\" method=\"post\" action=\"\">
				<label for=\"username\">Username:</label><br />
				<input type=\"text\" name=\"username\" id=\"signInFormUsername\" class=\"formField\" value=\"{$username}\" /><br /><br />
				<label for=\"password\">Password:</label><br />
				<input type=\"password\" name=\"password\" id=\"signInFormPassword\" class=\"formField\" /><br /><br />
				<input type=\"submit\" id=\"signInSubmit\" name=\"signInSubmit\" value=\"Login\" />
		</form>
		<br />
		{$error}
	";
}
}


?>

Link to comment
Share on other sites

To me it looks like you're still trying to redirect after you've called your header at require_once('header.php');

 

&

 

You're calling require_once('_coreFunctions.php'); outside the sif_loginForm function?

 

Yeah, this is my current code...

 

I cannot seem to find a way to call the function before HTML has been outputted due to the header and where in the HTML this function is being called.

 

I had a bit of a mess about with ob_start() and ob flush and that worked but that is more of a sticking plaster rather than a solution.

 

the _coreFunctions.php is being called in the whole find so it doesnt need to be re-called in the function.

Link to comment
Share on other sites

Your code is a little bit messy.

Have you got a real user name ?

Put this code inside the while loop:

while($row = mysqli_fetch_object($query))
		{
                               echo '<pre>'.print_r($row, true).'</pre>'; exit; 
			$_SESSION['firstName'] = $row->firstName;
		}

Link to comment
Share on other sites

Forget about the register form, it doesnt do anything at the moment and is just a placeholder for the form at the moment.

 

the username is an email address.

 

what does that code do?

 

Keep learning and come back later again.

Link to comment
Share on other sites

forget about the register form at the moment, it has no bearing on what I am trying to achieve.

 

yes does because you're calling the header on the register form. I don't think you know enough to make that statement. Like I said ...

 

I'd take everything out of the function until you get more experience. Also I'd put the register on one page and login in on another page.

Link to comment
Share on other sites

The problem is a very simple one, and explain in the sticky HEADER ERRORS - READ HERE BEFORE POSTING THEM. I recommend doing as the title says.

 

Also: Always use die () after using a header redirect. If you don't, PHP will continue to parse your script all the way to the end, and possibly cause some security issues (or some other damage).

 

Jazzman1: I'm afraid you're barking up the wrong tree on this one, the OP clearly demonstrated the problem in an earlier post.

Link to comment
Share on other sites

Jazzman1: I'm afraid you're barking up the wrong tree on this one, the OP clearly demonstrated the problem in an earlier post.

Aha, now I've got it. He doesn't make the differences between require_once('header.php') and header("Location: .")  ;)

Link to comment
Share on other sites

Also: Always use die () after using a header redirect. If you don't, PHP will continue to parse your script all the way to the end, and possibly cause some security issues (or some other damage).

 

Thanks for the tip :)

 

The sif_loginForm function is used on the signin.php inside the signInFormWrapper DIV

 

I have had a look at that artice and like I say I know why this is happening, just trying to get the best solution?

Link to comment
Share on other sites

Play with it some,

 

and

 

I'd take everything out of the function until you get more experience. Also I'd put the register on one page and login in on another page.

 

Keep playing if you have, if you haven't just play with it, it's really simple if you put the register & login on two pages. Once you see whats happening you can combine them.

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.