While designing sign up page I wish to reload page if user has submitted anything wrong in the fill up boxes. How can I reload page after the user presses submit button with a wrong entry ? (while verifying expected content through php)
You can use header() plus some _GET variables for telling the original page there were errors.
Form submit page:
<?php
//.... lots of code validation
if ($failed) {
header('Location: http://path.com/to/your/site/original_form.php?error=1');
}
?>
Form page:
<?php
if (isset($_GET['error']) {
echo "there was an error! Please fix it!";
}
?>
Try the following -
if (validation_failed) {
header("Location: http://yourserver.com/signup.php");
} else {
header("Location: http://yourserver.com/welcome.php");
}
PHP Header function
Related
I am developing a plugin where i have a form and a submit button. The form-action is the_permalink(). When the user clicks on the submit-button, a message ("hello") should be visible, which works fine with this code:
if(isset($_POST['submit']))
{
display();
}
function display()
{
echo "hello";
}
But if i refresh the page, the message is gone. Does anybody know how to fix this to that the message stays there?
This has porbably something to do with that i compare $_POST with 'submit', which is not given after a refresh...
Thanks for your help!
Please try following code
session_start();
if(isset($_POST['submit']))
{
$display_val = display();
$_SESSION['display_val'] = $display_val;
}
if(isset($_SESSION['display_val']))
{
echo $_SESSION['display_val'];
}
function display()
{
return "hello";
}
Below is my code, and is working fine.. The only problem is that when I refresh the page , It execute the success query.
<form action="" method="post">
<input type="submit" value="Purchase" id="btn" name="link_credits">
</form>
if(isset($_REQUEST['link_credits']))
{
echo "success";
//some codes here with sql statements
}
Else
{
echo " Please try Again tomorrow";
}
?>
Problem:
I don't want to run the query with page refresh, it should run the code only at button click.
Just add the line unset($_REQUEST['link_credits']); in success loop
if(isset($_REQUEST['link_credits']))
{
echo "success";
//some codes here with sql statements
if($success){
$_SESSION['msg']="Data Added successfully";
}
else{
$_SESSION['msg']="Data Not Added successfully.Please Check";
}
unset($_REQUEST['link_credits']);
header("Location:--somepage.php--");
}
else
{
echo " Please try Again tomorrow";
}
Then you can show the $_SESSION['msg'] value in --somepage.php-- and after showing use unset($_SESSION['msg']);
Your browser should be warning you that a page refresh will re-submit the form. So, you are clicking a button even when you refresh the page, or you are using a broken browser.
All that being said, here are some possible solutions:
Cookie the browser during the "success" phrase, and only allow the success condition to run if the cookie is not set/present.
Learn about "Redirect after POST", which is what you should be doing here. Essentially, you perform the success action, set flags as apropos, and forward the browser (via PHP's header()) to a page that says, "OK, it worked!".
Change your if condition to.
if(isset($_POST['link_credits'])){
echo "success";
//some codes here with sql statements
}
else
{
echo " Please try Again tomorrow";
}
This is because you are submitting the form to the same page with the method post, so when you refresh the page it novamento sent another request post.
Solution:
if(isset($_REQUEST['link_credits'])) {
echo "success";
$_REQUEST['link_credits'] = null;
} else {
echo " Please try Again tomorrow";
}
This happen because of simply on refresh it will submit your request again.
So the idea to solve this issue by cure its root of cause.
I mean we can set up one session variable inside the form and check it when update.
if($_SESSION["csrf_token"] == $_POST['csrf_token'] )
{
// submit data
}
//inside from
$_SESSION["csrf_token"] = md5(rand(0,10000000)).time();
<input type="hidden" name="csrf_token" value="
htmlspecialchars($_SESSION["csrf_token"]);">
My question is may be foolish. But I want to know, if there is any code that prevent the page opening if tried with link but opens only after . if it's link is from another page.
for example, if i have a form on page contact.php and user gets redirected to page contact_success.php.
Now I want, if anybody tried to open contact_success.php page directly from browser address bar, it should not be get opened. It should be get opened only after form submission from contact.php page..
A simple way is just to use an if check on the contact_success.php page like
if($_SERVER['REQUEST_METHOD'] = "POST"){
//display page
} else {
echo 'You cannot view this page.';
//add redirect, if you like
}
Set a session or a cookie for the user in the contact.php, and then in the contact_sucess.php you can check if it exists.
for exmaple:
contact.php:
<?php
$_SESSION['visited_login'] = 'yes';
?>
contact_sucess.php:
<?php
if (isset($_SESSION['visited_login']) && $_SESSION['visited_login']=="yes") {
// output tanks
} else {
echo "go away";
die;
}
?>
You can use session for that example:
let say when you submit the contact form there is a page where in you will process the user input and i will use(refer to the #1)
contact_process.php
$_SESSION['success'] = 'success';
(this code will create a session)
then once success in processing the user input there is another page like(refer to # 2)
contact_success.php
if(isset($_SESSION['success'])
{
//Output if it is success
}
else{
header('location:contact.php');
}
(this code will check if session is existing if yes the desired output will show and if not it should turn back to contact.php)
you can easily learn session here http://www.w3schools.com/php/php_sessions.asp
why my echo doesn't work here in this code?It goes directly to the function header().
if($result){
echo("<script> alert('User Successfull Added')</script>");
header('location:home.php');
}
In order to make your code example work, you need to put it inside an if/else condition like so, if you want it to work, but since I only see what you posted as code, am putting this in as an example:
<?php
if($result){
echo("<script>alert('User Successfully Added')</script>");
echo("<script>window.location = 'home.php';</script>");
}
else {
header('Location: elsewhere.php');
}
Actually thy echo is working. but we cant see the message because at the same time the page redirects to the home.php page.
If you want to alert to display the alert message use jquery post. or php session variable
Assuming you want to both display the alert & redirect to home.php, why don't you just try setting a SESSION (or other global variable) and then check if it's set at the start of the redirected page?
Such as:
<?php
session_start();
$_SESSION['usercreated']="Y";
header('Location: elsewhere.php');
?>
and then on home.php at the start of your php code:
<?php
session_start();
if(isset($_SESSION['usercreated'])){
echo("<script> alert('User Successfully Added')</script>");
}
//....
?>
Sorry, I can't add comment here. But I think what you want to do needs Java and replace alert() with confirm()
var r=confirm("User Successfull Added")
if (r==true) // if user click OK
{
window.location.assign("home.php")
}
else // user click Cancel
{
}
In my website i have used ajax at many places... But the problem is that the file, lets say some_ajax_file.php, is visible in the source code... I want that other users dont just type this in the url and go to this page... If they do so, they will then be redirected to another page...
I tried following code on that ajax page:
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$cur_page=curPageName();
and checked it
if($cur_page=="some_ajax_file.php")
//then redirect...
it is working if the type some_ajax_file.php in the url...but the problem is that the the ajax function where i used this some_ajax_file.php is not working....plz help me ....i m stuck....
You must have to search for $_SERVER['HTTP_X_REQUESTED_WITH']
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')
{
// I'm AJAX!
}
else
{
header();
die();
}
Create a Session at the time of Login
<?php
session_start();
// store session data
$_SESSION['Username']= Userid;
?>
and check the Session Available on that Page...
session_start();
if(empty($_SESSION['Username'])) {
//Redirect them to common page....
}
if the session availed then Show the page
Otherwise redirect them to login page....