Jump to content

PHP - header won't redirect - Need help

Okay so here is my code from the Login_check.php

-------------------------------------------------------------------------

<?php$errorUsername='';$errorPassword=''; if(isset($_POST['submit'])){if (empty($_POST['myusername'])) {$errorUsername = "Username is invalid";} if(empty($_POST['mypassword'])) {$errorPassword = "Password is invalid";} else{$host = "xxxxxx";$database = "xxxxxx";$username = "xxxxxx";$password = "xxxxxx";$table="xxxxxx"; // Table name $con= mysql_connect("$host","$username","$password") or die ("Unable to connect");mysql_select_db("$database") or die ("unable to connect to the database"); $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword'];  $myusername = stripslashes($myusername);$mypassword = stripslashes($mypassword);$myusername = mysql_real_escape_string($myusername);$mypassword = mysql_real_escape_string($mypassword);$sql="SELECT * FROM $table WHERE Username='$myusername' and Password='$mypassword'";$result=mysql_query($sql);  $user_count=mysql_num_rows($result);  if($user_count == 1){session_start();$_SESSION['login_user'] = $myusername;header("http://gamingglobal.comuf.com/index.php");}else {$errorUsername = "Username is invalid";}}}?>-------------------------------------------------------------------------------- This is my login form in the login.php-------------------------------------------------- <table width="990" border="0" align="center">            <tr>              <td align="left" valign="top"><table width="788" border="0" align="left">                <tr>                  <td height="84" valign="top" class="maincontent"><form name="form1" method="post" action="PHP files/Login_check.php">                    <p> </p>                    <table width="780" border="0">                      <tr>                        <td width="88">Username:</td>                        <td width="682"><label for="myusername"></label>                        <input name="myusername" type="text" id="myusername" size="60"></td>                      </tr>                      <tr>                        <td>Password:</td>                        <td><label for="mypassword"></label>                        <input name="mypassword" type="password" id="mypassword" size="60"></td>                      </tr>                      <tr>                        <td> </td>                        <td><input type="submit" name="submit" id="submit" value="Submit">                        <input type="reset" name="reset" id="reset" value="Reset"></td>                      </tr>                    </table>                    <p> </p>                  </form></td>                </tr>              </table>
-----------------------------------------------------------------------------
 
If you know why it won't redirect then please let me know. On another site it works fine (created the other one).
I know if I'm logged in because it I manually go back to the index page then the login hyperlink has changed to a log out and it only does that when it picks up a session :).
Edited by colonel_mortis
Code tags
Link to comment
https://linustechtips.com/topic/367925-php-header-wont-redirect-need-help/
Share on other sites

Link to post
Share on other sites

What are you trying to do exactly? Redirect to a webpage after you use this? use this instead:

header("url=URL_Replace")

It should work now.

 

Nope it doesn't redirect it to the index page :/ it just shows this when I click the submit button:

http://gamingglobal.comuf.com/PHP%20files/Login_check.php

 

This is what I have in the php file now:

header("url=http://gamingglobal.comuf.com/index.php");

Link to post
Share on other sites

Change

 

if($user_count == 1){session_start();$_SESSION['login_user'] = $myusername;header("http://gamingglobal.comuf.com/index.php");}

to

 

if($user_count == 1){session_start();$_SESSION['login_user'] = $myusername;header("Location: http://gamingglobal.comuf.com/index.php");}
Link to post
Share on other sites

 

Change

 

if($user_count == 1){session_start();$_SESSION['login_user'] = $myusername;header("http://gamingglobal.comuf.com/index.php");}

to

 

if($user_count == 1){session_start();$_SESSION['login_user'] = $myusername;header("Location: http://gamingglobal.comuf.com/index.php");}

 

That's what it was like, the formatting just messed up when I copied it across :/

 

EDIT: I'll be damned it worked... thanks.. I just recopied yours for verification and it worked...

Link to post
Share on other sites

That's what it was like, the formatting just messed up when I copied it across :/

 

EDIT: I'll be damned it worked... thanks.. I just recopied yours for verification and it worked...

 

You didn't define the header as a redirect by adding the "Location" class before the URL.

Link to post
Share on other sites

:( as I mentioned in pm... Please. Please. Please switch to using PDO or mysqli. :'(

 

this. in college i have to build a web based app for managing a rugby team and the server thinks it's in 2004 and is running such an old version on php i'm considering exploiting it lol

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

this. in college i have to build a web based app for managing a rugby team and the server thinks it's in 2004 and is running such an old version on php i'm considering exploiting it lol

 

Don't. I work for network operations at a college. Do not exploit their server. It will only cause trouble for you--including possible expulsion (yes, I know, it's dumb). Email your helpdesk/netops team and alert them.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Don't. I work for network operations at a college. Do not exploit their server. It will only cause trouble for you--including possible expulsion (yes, I know, it's dumb). Email your helpdesk/netops team and alert them.

 

Yeah i know i'm not planning on doing it but the college support is useless they are running ages old version on java, i've been there 2 years and still getting the same update....

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Yeah i know i'm not planning on doing it but the college support is useless they are running ages old version on java, i've been there 2 years and still getting the same update....

There's probably a reason. A ton of backend enterprise apps for college are incompatible with newer Java versions.

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×