PHP Header-Location Problem (Anfänger Issue)

Seite 1 von 3 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/php-header-location-problem-anfnger-issue_242176/page_1 - zur Vollversion wechseln!


Garrett schrieb am 21.02.2015 um 17:25

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 schrieb am 21.02.2015 um 17:35

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.


Garrett schrieb am 21.02.2015 um 17:40

Hab oben jetzt das hier geschrieben:

Code: PHP
if (isset($_GET['success']))
Funkt leider noch immer nicht. :(


AdRy schrieb am 21.02.2015 um 17:54

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.


Garrett schrieb am 21.02.2015 um 18:00

Ich versteh halt net, warum es bei dem hier funktioniert...
https://www.youtube.com/watch?featu...50qmC7wFo#t=283


AdRy schrieb am 21.02.2015 um 18:02

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


COLOSSUS schrieb am 21.02.2015 um 18:21

Es ist (unabh. von Deinem spezifischen Problem) grundfalsch, sowas per GET-Parameter zu erledigen.


Garrett schrieb am 21.02.2015 um 18:24

Zitat von COLOSSUS
Es ist (unabh. von Deinem spezifischen Problem) grundfalsch, sowas per GET-Parameter zu erledigen.
Bin für alles offen. :)


Nico schrieb am 21.02.2015 um 18:28

kommt am ende der url dann das ?success im browser?


Garrett schrieb am 21.02.2015 um 18:37

Zitat von Nico
kommt am ende der url dann das ?success im browser?
Nope. :-(


Garrett schrieb am 21.02.2015 um 18:39

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 schrieb am 21.02.2015 um 18:42

dann wird die location auch nie geändert und deine abfrage ist nicht schuld.


Garrett schrieb am 21.02.2015 um 18:51

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 schrieb am 21.02.2015 um 18:51

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 schrieb am 22.02.2015 um 09:26

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.




overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2026