Alternative header for redirects in PHP

I am working on a project and I need to run my program on another web server. This is a pretty simple login page that I'm having a problem with. The program works correctly if I run it through the local host through WAMP. The problem I encountered is that part of the redirect is not working properly, it checks the user and starts the session, but when he receives the redirect, nothing happens.

I either do something wrong with my syntax, which, I think, is unlikely, since it works correctly through my local host. Or, alternatively, I think that the server does not have this function (I'm not sure that you can choose which modules are supported by your server, although I am sure that this is possible).

I don’t know if it matters, but they use "cpanel", where I can access the files and everything is in the same directory, so if someone can tell me where I am wrong, or offer an alternative to redirecting via "header "any help would be greatly appreciated. I looked around, but it seems that the "headline" is a standard workhorse.

Here is the code I have:

if( (!empty($_POST['username'])) && (!empty($_POST['password'])) ) { // username and password sent from Form $myusername = $_POST['username']; $mypassword = $_POST['password']; $sql="SELECT UserName FROM User WHERE UserName='$myusername' and Password='$mypassword'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); //$active=$row['active']; $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1) { echo "we made if to the if"; session_start(); session_register("myusername"); $_SESSION['login_user']=$myusername; echo "right b4 the re-direct"; header("location: UI.php"); exit; } else echo "Your user name/password was not correct pleast TRY AGAIN!!!"; } 

Update: in response to the statements about the echo, there would be a problem that I process my form in the same file and use echo $ _SERVER ['PHP_SELF']

+8
redirect php
source share
7 answers

From the docs :

Remember that header() must be called before sending any actual output, either using regular HTML tags, empty lines in a file, or from PHP.

Your echo causes the redirect to fail, like any other output sent before the header.

+1
source share

I use this function to redirect ...

What works in all situations. Even if headers are already sent ... or even javascript is disabled ..

 function redirect($url) { if (!headers_sent()) { header('Location: '.$url); exit; } else { echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit; } } 
+37
source share

Using the code below, we redirect the page

 $page = $_SERVER['REQUEST_URI']; echo '<script type="text/javascript">'; echo 'window.location.href="'.$page.'";'; echo '</script>'; 
+2
source share

You cannot set headers after some text is output, so either you move the header call in front of the echo statements (fine in your case), or use output buffering .

+1
source share

Redirecting through headers is illegal if any content has passed. Take out all the echo and it should work. If you want the message displayed to the user before redirecting, you will probably have to do this using JavaScript via location.href .

0
source share

You probably already sent the content to the browser before sending the php header. It could just be \ n after closing the php tag (?>)

To find the place where unwanted output is generated, it can be difficult to use large projects or long lines of classes that extend each other. To find the problem, you can do this:

  error_reporting(E_ALL); ini_set("display_errors", 1); header("Location: https://mydomain.com/myLoginAdress/"); die(); 

Strict reporting of errors will cause a line and an output file.

0
source share
 echo "<script>window.location.href='yourPage.php'</script>"; 
0
source share

All Articles