Jump to content

[SOLVED] Redirect on Login


antwonw

Recommended Posts

Hey,

 

So I have implemented a login script to my site. The only problem is when it loads it refreshes the page and displays "Welcome $username! You are logged in! Visit the Galleries Page!"

 

I tried using a "header('Location: /galleries/');" to redirect on login but I couldn't get it to work.

 

Here is my login page

 

login.php

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

$error = '0';

if (isset($_POST['submitBtn'])){
// Get user input
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
        
// Try to login the user
$error = loginUser($username,$password);
}

?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Galleries</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/login.css" />
</head>
<body bgcolor="000000">

<table id="rotator" border="0" width="100%" height="100%">
<tr>
	<td align="center">
<table id="loginbox" border="0" width="339" height="339">
<tr>
	<td>
		<table border="0" background="" width="280" height="280" align="center">
			<form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<tr>
				<td colspan="2" height="40px">
					 
				</td>
			</tr>
			<tr>
				<td align="center" colspan="2">
					<h2>Login</h2>
				</td>
			</tr>
			<tr height="5px">
				<td align="center">
					<font color="#ffffff">
					Username:
					</font>
				</td>
				<td>
					<input class="text" type="text" name="username" size="18" maxlength="18">
				</td>
			</tr>
			<tr height="5px">
				<td align="center">
					<font color="#ffffff">
					Password:
					</font>
				</td>
				<td>
					<input class="text" type="password" name="password" size="18" maxlength="18">
				</td>
			</tr>
		    <tr height="60px"> 
		      <td align="center" colspan="2"> 
		          <input class="text" type="submit" name="submitBtn" value="Login">
		          <input class="text" type="reset" name="Clear" value="Clear">
		      </td>
		  </tr>
			<tr>
				<td align="center" colspan="2" height="50px">
					<?php   
					    if (isset($_POST['submitBtn'])){

					?>
					<?php
						if ($error == '') {
							echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
							echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>';
						}
						else echo $error;

					?>
					<?php            
					    }
					?>
				</td>
			</tr>
		</form>
		</table>
	</td>
</tr>
</table>

	</td>
</tr>
</table>
</body>
</html>

 

Here is the common.php file

 

common.php

<?php

session_start();

function registerUser($user,$pass1,$pass2){
$errorText = '';

// Check passwords
if ($pass1 != $pass2) $errorText = "Passwords are not identical!";
elseif (strlen($pass1) < 6) $errorText = "Password is to short!";

// Check user existance	
$pfile = fopen("pwd.asp","a+");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            $errorText = "The selected user name is taken!";
            break;
        }
    }

    // If everything is OK -> store user data
    if ($errorText == ''){
	// Secure password string
	$userpass = md5($pass1);
    	
	fwrite($pfile, "\r\n$user:$userpass");
    }
    
    fclose($pfile);


return $errorText;
}

function loginUser($user,$pass){
$errorText = '';
$validUser = false;

// Check user existance	
$pfile = fopen("pwd.asp","r");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            // User exists, check password
            if (trim($tmp[1]) == trim(md5($pass))){
            	$validUser= true;
            	$_SESSION['userName'] = $user;
            }
            break;
        }
    }
    fclose($pfile);

    if ($validUser != true) $errorText = "<font color='red'>Invalid username or password!</font>";
    
    if ($validUser == true) $_SESSION['validUser'] = true;
    else $_SESSION['validUser'] = false;

return $errorText;	
}

function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['userName']);
}

function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
	header('Location: ../login.php');
}
}

?>

 

I still want it to display "Invalid username or password!" and the other error's on the login.php page. But I want a successful login to redirect to "galleries.php"

 

Can anyone help me with this. I can't seem figure this one out.

 

Thanks!

 

AW

Link to comment
Share on other sites

In your login.php replace this

 

                 <?php   
                      if (isset($_POST['submitBtn'])){

                  ?>
                  <?php
                     if ($error == '') {
                        echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
                        echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>';
                     }
                     else echo $error;

                  ?>
                  <?php            
                      }
                  ?>

 

with this

 

<?php   

if (isset($_POST['submitBtn'])) {
  if ($error == '') {
    echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
    sleep(3); //Pause for 3 Seconds
    header("Location: ./galleries/"); //Redirect to Galleries
  } else { echo $error; }
}

?>

Link to comment
Share on other sites

In your login.php replace this

 

                 <?php   
                      if (isset($_POST['submitBtn'])){

                  ?>
                  <?php
                     if ($error == '') {
                        echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
                        echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>';
                     }
                     else echo $error;

                  ?>
                  <?php            
                      }
                  ?>

 

with this

 

<?php   

if (isset($_POST['submitBtn'])) {
  if ($error == '') {
    echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
    sleep(3); //Pause for 3 Seconds
    header("Location: ./galleries/"); //Redirect to Galleries
  } else { echo $error; }
}

?>

 

This did not work. I got a return error of the follow:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\site.org\httpdocs\login.php:28) in C:\Inetpub\vhosts\site.org\httpdocs\login.php on line 77

 

Any suggestions?

 

Thanks!

 

AW

Link to comment
Share on other sites

<?php   

if (isset($_POST['submitBtn'])) {
  if ($error == '') {
    ob_start();
    echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
    sleep(3); //Pause for 3 Seconds
    header("Location: ./galleries/"); //Redirect to Galleries
    ob_flush();
  } else { echo $error; }
}

?>

 

Output buffering, because you can't use header() after any output has been sent.

Link to comment
Share on other sites

<?php   

if (isset($_POST['submitBtn'])) {
  if ($error == '') {
    ob_start();
    echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3";
    sleep(3); //Pause for 3 Seconds
    header("Location: ./galleries/"); //Redirect to Galleries
    ob_flush();
  } else { echo $error; }
}

?>

 

Output buffering, because you can't use header() after any output has been sent.

 

I still got the same thing!

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\site.org\httpdocs\login.php:28) in C:\Inetpub\vhosts\site.org\httpdocs\login.php on line 78

 

Link to comment
Share on other sites

So does anyone have a suggestion for another PHP Authentication Application that is purely PHP. I can't do SQL/Databases at all or else I would. Any ideas or suggestions?

 

I also need it to redirect to a different page once login.

 

Thanks!

Link to comment
Share on other sites

The rules are simple. You cannot use the header function once output has been sent to the browser.

 

Organise your code in such a way as to ensure that and you'll be fine.

 

Problem is I can read and understand PHP to a limit but writing it is another thing for me. I'm self taught and still learning.

Link to comment
Share on other sites

Problem is I can read and understand PHP to a limit but writing it is another thing for me. I'm self taught and still learning.

 

Most everyone is self taught in regard to php, and  great way to learn is to fix problematic code.

 

So I take it no one can help me out or at least point me in the right direction?

 

You've been given a push in the right direction. Without writing the code for you there isn't much more we can do.

Link to comment
Share on other sites

Instead of using the PHP header() you can try another method using JavaScript

 

$redirectUrl = "http://localhost/www/target-redirect.php";
print "<script type=\"text/javascript\">";
print "window.location.href = '$redirectUrl'";
print "</script>";

 

Had the same problem some days ago.

Link to comment
Share on other sites

  • 2 weeks later...

AWESOME!!!

 

Thanks for the help!!! Here is what I did.

 

if ($error == '') {
	$redirectUrl = "/galleries";
	print "<script type=\"text/javascript\">";
	print "window.location.href = '$redirectUrl'";
	print "</script>";
}
else echo $error;

 

Again, thanks for the help!!!

 

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.