"Christmas - the time to fix the computers of your loved ones" « Lord Wyrm

PHP Header-Location Problem (Anfänger Issue)

Garrett 21.02.2015 - 17:25 6490 30
Posts

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Hi Leute,

dieser Code ist ein Teil meiner register.php. Also die Seite, wo sich User regisitrieren.
Ich hätte gerne, dass nach Erfolgreichem Ausfüllen und Abschicken des Formulars die Funktion register_user($register_data) ausgeführt wird. Das funkt auch super und der Eintrag in die SQL DB geht auch problemlos.

Mein Problem: Ich will, dass man danach auf register.php?success weitergeleitet wird, wo der User dann zu lesen bekommt "You have been registered successfully!".
Allerdings funkt das nicht, konkret wird gar nix angezeigt. Der Code ist von einem YT Tutorial und ich hab ihn schon zig mal kontrolliert - ich hab einfach keine Ahnung warums bei mir nicht funkt, beim Tutorial aber schon. Jemand irgendwelche Ideen?

Vielen Dank!

Code: PHP
			
<?php
	

if (isset($_GET['success']) && empty($_GET['success'])) {
	
	echo 'You have been registered successfully!'; //Warum funktioniert das nicht? YT Part 11
	
} else {

		if (empty($_POST) === false && empty($errors) === true){
		$register_data = array(
			'username' 	=> $_POST['username'],
			'password' 	=> $_POST['password'],
			'first_name'	=> $_POST['first_name'],
			'last_name' 	=> $_POST['last_name'],
			'email' 	=> $_POST['email']
		);	
		
		
		register_user($register_data);
		header('Location: register.php?success'); //Warum funktioniert das nicht? YT Part 11
		exit();
		
		
	} else if (empty($errors) === false) {
		
		echo output_errors($errors);
	}

}


?>

AdRy

Auferstanden
Avatar
Registered: Oct 2002
Location: Wien
Posts: 5239
lass dir mal $success ausgeben. isset() gibt nur true zurück wenn ein wert drinnensteht (außer "false") aber empty() gibt true zurück wenn sie "false" ist. Beide Bedingungen können nicht gleichzeitg zutreffen ( && ) deswegen wirds nie ausgeführt.

lass doch eifnach auf register.php?success=1 weiterleiten und pass die Abfrage an.

Vl. sind noch andere Fehler drinnen.
Bearbeitet von AdRy am 21.02.2015, 17:38

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Hab oben jetzt das hier geschrieben:
Code: PHP
if (isset($_GET['success']))
Funkt leider noch immer nicht. :(

AdRy

Auferstanden
Avatar
Registered: Oct 2002
Location: Wien
Posts: 5239
Wenn du auf register.php?success' gehst dann ist (isset($_GET['success'])) NICHT true -> if statement wird nicht ausgeführt.

Du musst success einen wert zuweisen. Siehe oben.
Bearbeitet von AdRy am 21.02.2015, 17:56

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Ich versteh halt net, warum es bei dem hier funktioniert...
https://www.youtube.com/watch?featu...50qmC7wFo#t=283

AdRy

Auferstanden
Avatar
Registered: Oct 2002
Location: Wien
Posts: 5239
kleines Bsp:

Code: PHP
<?php

if ($_GET['success']==1){
echo ("done!");

}
else if(!isset($_GET['success'])){
header('Location: 1.php?success=1'); //Warum funktioniert das nicht? YT Part 11
exit();
}
?>

Geht bei mir

Warums bei dem geht ka, muss auf einen php profi warten. Bin selber nur Pfuscher :D
Bearbeitet von AdRy am 21.02.2015, 18:04

COLOSSUS

Administrator
Frickler
Avatar
Registered: Dec 2000
Location: ~
Posts: 11902
Es ist (unabh. von Deinem spezifischen Problem) grundfalsch, sowas per GET-Parameter zu erledigen.

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Zitat von COLOSSUS
Es ist (unabh. von Deinem spezifischen Problem) grundfalsch, sowas per GET-Parameter zu erledigen.
Bin für alles offen. :)

Nico

former person of interest
Registered: Sep 2006
Location: -
Posts: 4082
kommt am ende der url dann das ?success im browser?

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Zitat von Nico
kommt am ende der url dann das ?success im browser?
Nope. :-(

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Hier ist vollständigkeitshalber der gesamte code dieser register.php

Code: PHP
<?php
include 'core/init.php';
include 'includes/overall/header.php';


if(empty($_POST) === false){
	$required_fields = array('username','password','password_again','first_name','email');
	foreach($_POST as $key=>$value){
		if(empty($value) && in_array($key, $required_fields) === true){
			$errors[] = 'Fields marked with a star are required';
			break 1;
		}
	}

	if(empty($errors) === true){
		if(user_exists($_POST['username']) === true){
			$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\'  is already taken.';
		}
		
		if(preg_match("/\\s/", $_POST['username']) == true){
			$errors[] = 'Your username must not contain any spaces.';
		}
		
		if (strlen($_POST['password']) < 6 ){
			$errors[] = 'Your password must be at least 6 characters';
		}
	
		if($_POST['password'] !== $_POST['password_again']){
			$errors[] = 'Your passwords do not match';
		}
		
		if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
			$errors[] = 'A valid email address is required';
		}
		
		if(email_exists($_POST['email']) === true){
			$errors[] = 'Sorry, the email \'' . $_POST['email'] . '\'  is already in use.';	
		}
		
	
	}
}

?>	
			<h1>Register</h1>
			
<?php
	

if (isset($_GET['success'])) {
	
	echo 'You have been registered successfully!'; //Warum funktioniert das nicht? YT Part 11
	
} else {

		if (empty($_POST) === false && empty($errors) === true){
		$register_data = array(
			'username' 		=> $_POST['username'],
			'password' 		=> $_POST['password'],
			'first_name'	=> $_POST['first_name'],
			'last_name' 	=> $_POST['last_name'],
			'email' 		=> $_POST['email']
		);	
		
		
		register_user($register_data);
		header('Location: register.php?success'); //Warum funktioniert das nicht? YT Part 11
		exit();
		
		
	} else if (empty($errors) === false) {
		
		echo output_errors($errors);
	}

}





?>
			
<form action="" method="post">
	<ul>
		<li>
			Username*:<br>
			<input type="text" name="username">
		</li>
		<li>
			Password*:<br>
			<input type="password" name="password">
		</li>
		<li>
			Password again*:<br>
			<input type="password" name="password_again">
		</li>
		
		<li>
			First name*:<br>
			<input type="text" name="first_name">
		</li>
		<li>
			Last name:<br>
			<input type="text" name="last_name">
		</li>	
		<li>
			Email*:<br>
			<input type="text" name="email">
		</li>
		<li>
			<input type="submit" value="Register">
		</li>			
			
	</ul>
</form>

<?php


?>	
			
			
<?php
include 'includes/overall/footer.php';
?>

Nico

former person of interest
Registered: Sep 2006
Location: -
Posts: 4082
dann wird die location auch nie geändert und deine abfrage ist nicht schuld.

Garrett

Here to stay
Avatar
Registered: May 2003
Location: Wien
Posts: 1079
Zitat von Nico
dann wird die location auch nie geändert und deine abfrage ist nicht schuld.
Eben, drum hab ich im Threadtitel eh geschrieben, dass ich ein "Header-Location" Problem habe. :)

AdRy

Auferstanden
Avatar
Registered: Oct 2002
Location: Wien
Posts: 5239
Zitat
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
http://php.net/manual/en/function.header.php

kleinerChemiker

Here to stay
Avatar
Registered: Feb 2002
Location: Wien
Posts: 4256
Eigentlich müßtest du auch von PHP einen Fehler bekommen: headers already sent
beim entwickeln empfiehlt es sich auch, möglichst alle fehler anzeigen zu lassen.
Kontakt | Unser Forum | Über overclockers.at | Impressum | Datenschutz