n3xtgensystem Posted December 3, 2011 Share Posted December 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/ Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 For one, you should read the sticky on the forum about header errors. Another, you should check out header in the manual, and probably Google more on HTTP headers and acceptable values. Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294005 Share on other sites More sharing options...
QuickOldCar Posted December 3, 2011 Share Posted December 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294009 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 Because that's not how you redirect. This is how: header("Location: menu.html?memberId=" . $memberId); Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294016 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294024 Share on other sites More sharing options...
n3xtgensystem Posted December 3, 2011 Author Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294029 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 Hmm. That shouldn't happen. If the header wasn't sent, the page should redirect thus changing the URL. Does the same thing happen with this? header('redirect:0; url=menu.html'); Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294034 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 That seems like client side behavior. When I use header( 'Location: ...' ) the destination URI shows up in my browser. Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294036 Share on other sites More sharing options...
dweeber Posted December 4, 2011 Share Posted December 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294368 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 placing exit; at the end is not always a good idea, you may want to continue processing the script Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294369 Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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; } } }); Quote Link to comment https://forums.phpfreaks.com/topic/252397-php-header-redirect-not-working/#findComment-1294515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.