Jump to content

I know the header needs to be at the top but how do I get round this problem?


dmccabe

Recommended Posts

I am working on a registration script and all is working well, however when the user clicks the link in the  email to validate their account, I want it to redirect them to the profile.php page, however I get the modified headers already sent message and realise this becasue the header has to be at the top, so how to get round this?

 

<?php
SESSION_START();

include('dbconnect.php');

if (isset($_GET['vcode']) && !empty($_GET['vcode'])) {
$vcode = $_GET['vcode'];
$uid = $_GET['user'];
$query = "SELECT `validation_code` FROM `users` WHERE `ID` = '$uid'";
$result = mysql_query($query);
if ($result) { 
	$row = mysql_fetch_row($result);
	$vcode_var = $row[0];
	if ($vcode == $vcode_var) {
		$_SESSION['uid'] = $uid;
		header( 'profile.php' ) ;
	} else {
		echo "Failure!";

	}	
} else {
	die(mysql_error());
}
}

Given that code, I do not see where it has gone wrong, check that you do not have a whitespace before the <?php as that will cause header errors. Also the correct way to do a header redirect is as follows:

 

         header( 'Location: profile.php' ) ;

The error is this:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/techmonk/public_html/projects/freelance/dbconnect.php:17) in /home/techmonk/public_html/projects/freelance/register.php on line 16

 

THis is dbconnect.php

 

<?
/*--------- DATABASE CONNECTION INFO---------*/
$hostname="localhost";
$mysql_login="";
$mysql_password="";
$database="";

// connect to the database server
if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
  die("Can't connect to database server.");    
}else{
  // select a database
    if (!(mysql_select_db("$database",$db))){
      die("Can't connect to database.");
    }
}
?> 

 

and this is the register.php again:

 

<?php
SESSION_START();

include('dbconnect.php');

if (isset($_GET['vcode']) && !empty($_GET['vcode'])) {
$vcode = $_GET['vcode'];
$uid = $_GET['user'];
$query = "SELECT `validation_code` FROM `users` WHERE `ID` = '$uid'";
$result = mysql_query($query);
if ($result) { 
	$row = mysql_fetch_row($result);
	$vcode_var = $row[0];
	if ($vcode == $vcode_var) {
		$_SESSION['uid'] = $uid;
		header( 'profile.php' ) ;
	} else {
		echo "Failure!";

	}	
} else {
	die(mysql_error());
}
}

 

There is stuff after this, but nothing before it and no white spaces

The same goes for the dbconnect, there cannot be a white space there at the top or bottom. Any whitespace in there will trinkle down. Since Line 16 in the DB file is right at the ?> that is my best guess.

Also the correct way to do a header redirect is as follows:

 

         header( 'Location: profile.php' ) ;

 

Did you change that as suggested? From the code you posted above, does not seem like it.

ah I missed the bit about the Location you were right thanks.

 

However I now have a new problem I get this:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/techmonk/public_html/projects/freelance/register.php on line 37

 

After making a change to check they weren't already validated:

 

<?php
SESSION_START();
include('dbconnect.php');
if (isset($_GET['vcode']) && !empty($_GET['vcode'])) {
$vcode = $_GET['vcode'];
$uid = $_GET['user'];
$query = "SELECT `isactive`,`validation_code` FROM `users` WHERE `ID` = '$uid'";
$result = mysql_query($query);
if ($result) { 
	$row = mysql_fetch_row($result);
	$vcode_var = $row['validation_code'];
	$isactive = $row['isactive'];
	if ($isactive < 1) {
		if ($vcode == $vcode_var) {
			$query = "UPDATE `users` SET `isactive` = '1' WHERE `ID` = '$uid'";
			if (!mysql_query($query)) {
				die(mysql_error());
			} else {
				$_SESSION['uid'] = $uid;
				header( 'Location: profile.php' ) ;
			}
	`	} else {
			echo "Authentication failure, there may be a problem with the website, please click here to contact an admin.";
		}	
	} else {
		echo "The user requested is already active, please click here to login or here to have your username and password sent to you.";
	}
} else {
	die(mysql_error());
}
}

$usererror = 0;
$passerror = 0;
$emailerror = 0;

if (isset($_POST['submit'])) {  <---- this is line 37
if (empty($_POST['username'])) {
	echo "<font color='#FF0000'>You must specify a username!<br /></font>";
	$usererror = 1;
} elseif (empty($_POST['password'])) {
	echo "<font color='#FF0000'>You must specify a password!<br /></font>";
	$passerror = 1;
} elseif ($_POST['password'] != $_POST['confirm_password']) {
	echo "<font color='#FF0000'>Your password and confirmation do not match!<br /></font>";
	$passerror = 1;
} elseif (!preg_match("/^[^@]*@[^@]*\.[^@]*$/", $_POST['email'])) {
        echo "<font color='#FF0000'>Invalid E-mail address, please try again!</font><br />";
	$emailerror = 1;		
    }

 

I have indicated line 37, but I have not modified that line nor any of the other lines since posting in this thread so dont see why the error is there?

      `   } else {
            echo "Authentication failure, there may be a problem with the website, please click here to contact an admin.";
         }   

 

You have a weird ` character there, perhaps that is causing all the fuss...?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.