-1

I need a help about redirection. I have a form that is written in HTML simple form. It is getting username and password and posting them to another php file. This PHP file is checking user and password if it is false; I want to redirect to back form file in order to get username and pasword again. But before redirection to form page, I want to show the user there is error and redirectin message. Maybe, we can show this message in form page, maybe before redirection. How can we handle this ? COuld you please help me for this?

FORM FILE:

<html>
<body>

<form action="cvphost.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>

</body>
</html>

PHP FILE:

<pre>
<?php
#GETTING EXISTING HOST FILE OF ANSIBLE
$host_file=file('/etc/ansible/hosts');
#GETTING VALUE OF USERNAME,PASS LINE OF HOSTFILE
$ansibleusername=$host_file[7];
$ansiblepassword=$host_file[8];
#PARSING USERNAME AND PASS LINE OF THAT HOST FILE
$list1=split('=',$ansibleusername);
$list2=split('=',$ansiblepassword);
#UPDATING USERNAME AND PASS FROM FORM
$list1[1]=$_POST["username"];
$list2[1]=$_POST["password"];
#JOINING WITH NEW VALUE AND INSERTING HOST-FILE LINES
$array1=array($list1[0],$list1[1]);
$array2=array($list2[0],$list2[1]);
$newansibleusername=join("=",$array1);
$newansiblepassword=join("=",$array2);
$host_file[7]=$newansibleusername."\n";
$host_file[8]=$newansiblepassword."\n";
#PUSHING NEW FILE TO ANSIBLE HOST FILE
$updated_host_file=file_put_contents('/etc/ansible/hosts',$host_file, LOCK_EX);
#usleep(1000);
#chdir('/etc/ansible/');
#$pwd=shell_exec('pwd');
#$task=shell_exec('ansible-playbook user-login-playbook.yml');
#echo $task;
#echo $pwd;
$log_file=file('/etc/ansible/log/user-login-playbook-log.txt');
***#THIS PART IS REDIRECTION PART OF MY CODE. ABOVE IS CORRECT, I WANT FIX BELOW PART***
if ($log_file[0]=="YOU CAN MANAGE CVP NOW")
   { print_r ($log_file[0]);}
else
 {  print_r ($log_file[1])."\n";
    print "<pre>";
    print_r('REDIRECTING TO LOGIN PAGE!...');
}**
  • 1
    This is what you are looking for: https://www.php.net/manual/en/function.header.php – Mech Mar 29 '20 at 20:31

2 Answers2

0

Add a header to your redirect condition:

header('Location: /login.php');
Mech
  • 3,952
  • 2
  • 14
  • 25
  • If I add header, user cannot see php page with error message.It is only refreshing form page. When username/pass are wrong, I should see error message.Thıs error message can be seen in php page or form page, no matter. I now prefere see it on php page. then redirection to back form page. – Mahmut Aydin Mar 29 '20 at 20:38
-1

What you can do is create a file that will handle redirects and have the print print_r('REDIRECTING TO LOGIN PAGE!...'); and after that you can use the sleep function here and redirect them to the desired page

Your file would look like this:

print_r('REDIRECTING TO LOGIN PAGE!...');
sleep(5); //in seconds
header('Location: /a/location')
  • how can user see again form page after see REDIRECTING TO LOGIN PAGE? – Mahmut Aydin Mar 29 '20 at 20:42
  • 1
    another way of doing this is using `javascript` which will be in the user end, CLICKING THE BACK BUTTON ON THE BROWSER –  Mar 29 '20 at 20:44