How do I redirect the webpage in the code below?

I’ve tried using the code below but I’m stuck as to what I need to add to make it redirect.

This is what I’ve got so far:

<?php
	
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				
	function get_data() {
		$name = $_POST['name'];
		$file_name='login-data'. '.json';

		if(file_exists("$file_name")) {
			$current_data=file_get_contents("$file_name");
			$array_data=json_decode($current_data, true);
							
			$extra=array(
				'Username' => $_POST['Username'],
				'Password' => $_POST['Password'],
                'LoginDate' => $_POST['LoginDate'],
                'LoginTime' => $_POST['LoginTime'],
			);
			$array_data[]=$extra;
			echo "";
			return json_encode($array_data);
		}
		else {
			$datae=array();
			$datae[]=array(
				'Username' => $_POST['Username'],
				'Password' => $_POST['Password'],
                'LoginDate' => $_POST['LoginDate'],
                        'LoginTime' => $_POST['LoginTime'],
			);
			return json_encode($datae);
		}
	}

	$file_name='login-data'. '.json';
	
	if(file_put_contents("$file_name", get_data())) {
		echo '';
	}				
	else {
		echo '';				
	}
}	
?>

To the same php page that this is already on. I want it to seem like it doesn’t redirect. It needs to redirect in order to send data to json file but I already have that data. It should be under this:

if(file_put_contents("$file_name", get_data())) {

Hi, here is the code may be helpful to you to redirection from your code.

if(file_put_contents("$file_name", get_data())) {
	echo ''; // Redirection will be here...
}				
else {
    echo '';				
}

Redirection concept in PHP

// Redirect to a specific URL
header("Location: http://example.com");

// Redirect to a relative URL
header("Location: /newpage.php");

// Redirect with a specific HTTP status code
header("Location: http://example.com", true, 301);
exit; // Make sure to exit after sending the header

Learn More in PHP

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.