Jump to content

Recommended Posts

I have an html login page. Once a user is verified through the PHP page they get redirected to another page. For some reason once I have validated that the user exists I get the message that is was successful but the page will not redirect. This is my script I use on my HTML page to pass the parameters:

 

			$(document).ready(function() {
			$('#submit').click(function() {
				var username = $('input[name=username]');
				var password = $('input[name=password]');
				var data = 'username=' + username.val() + '&password=' + password.val();

				.ajax({
					url: "login.php",
					type: "GET",
					data: data,
					cache: false,
					success: function() {
						alert("SUCCESS");
					}
				});
				return false;
			});
		});

 

And this is my PHP page:

 

			<?php	

			$username = ($_GET['username']) ? $_GET['username'] : $_POST['username'];
			$password = ($_GET['password']) ? $_GET['password'] : $_POST['password'];

			if ($_POST) $post=1;

			$userFound = false;
			$memberId = "";

			// Create connection string, Note, params are as follow
			// mysql_connect(databaseLocation, username, password)
			$con = mysql_connect("localhost","root","");
			if (!$con)
			  {
			  die("Could not connect: " . mysql_error());
			  }
			mysql_select_db("memberdb", $con); 
			// SQL Query
			$result = mysql_query("SELECT memberId, firstName, lastName, username, password FROM member");
				while($row = mysql_fetch_array($result))
				{
					if ($username == $row["username"] && $password == $row["password"])
					{
						echo "<h1>Welcome " . $row["firstName"] ." ". $row["lastName"]  . "! </h1></br>";
						$userFound = true;
						$memberId = $row["memberId"];
					}
				}
			// If User is found, continue on to next page, if not redirect to index.html
			if ($userFound)
			{
				echo "<h2>Login Successful</h2>";
				echo "<h3>Page will redirect</h3>";
				header( "url=menu.html?memberId=" . $memberId);
			}
			else
			{
				echo "<h2>LOGIN FAILED</h2>";
				echo "<h3>Page will redirect</h3>";
				header( "refresh: 1; url=index.html#loginPage");
			}					
		?>

 

It does the echo successfully but hangs on the part where is says :

 

header( "url=menu.html?memberId=" . $memberId);

 

How can I get it to redirect?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/
Share on other sites

You can not echo/display out anything before calling header()

 

absolutely nothing

 

I'll refer you to this page for more on it

http://www.phpfreaks.com/forums/index.php?topic=37442.0

 

also, does you html page read php code for that $_GET request of memberId?

 

if not edit your .htacess with AddType application/x-httpd-php .htm .html , save and restart the server

From the PHP.net manual on header()

 

Note:

 

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

 

<?php

/* Redirect to a different page in the current directory that was requested */

$host  = $_SERVER['HTTP_HOST'];

$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

$extra = 'mypage.php';

header("Location: http://$host$uri/$extra");

exit;

?>

Thanks. I have read the sticky and manual and tried to use:

 

header("Location: menu.htm");

 

$host  = $_SERVER['HTTP_HOST'];

$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');

$extra = 'menu.html';

header("Location: http://$host$uri/$extra");

exit;

 

And it will redirect to the correct page the URL does not change. It still says its on the login.php page even though it showing the menu.html page. I have tried other browsers to see if it works but still the same thing. :(

It is always a good idea to place an exit after the header command to prevent further action in the script.  Header outputs the header, but does not stop the processing of the rest of the script.

 

<?php ...

// Prevent non-logged in users from issuing commands.
if (! $loggedInUser) {
header( "Location: /login.php");
exit;
}

// User is logged in, make changes requested (like delete account)

 

If you don't, the code will continue on behind the scenes which might be a big surprise if you don't know it.

Your header redirect is not working because your processing the form using ajax.  Your header redirect is just going to cause the background ajax request to be redirect, not the page that is displayed.  If you want to redirect the page displayed, you need to output something which your ajax success handler can pick up and test for and then use JS to do your redirect.

 

ex:

echo json_encode(array(
   'response' => 'redirect',
   'url' => 'http://www.example.com'
));

 

				$.ajax({
					url: "login.php",
					type: "GET",
					data: data,
					cache: false,
					success: function() {
						//Get the json response via some means
						//then...
						if (jsonObj.response == 'redirect'){
						      window.location.href = jsonObj.url;
						}
					}
				});

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.