Home > Software engineering >  how to save variables after using header function php
how to save variables after using header function php

Time:12-28

After i submit a form and save the info to a database i use the header function to redirect to a more user friendly url but the variable $checkError is not saving, it gets reset after the header redirect. How can i save the variable even if the page gets refreshed?

if(isset($_GET['submit'])){
    
// get the post records <<
$id = '2';
$title = $_GET['title'];
$link = $_GET['link'];


// database connection
$con = mysqli_connect("localhost", "user", "password", "db_test");

$sql = "INSERT INTO test (id, title, link)
VALUES ($id, '$title', '$link')";

$rs = mysqli_query($con, $sql);


if($rs){
    $checkError = '<div >Success!</div>';
}else{
    $checkError = '<div >Not working!</div>';
}

mysqli_close($con);


//redirect to user friendly url
header("Location: /index.php?id={$id}");

}

CodePudding user response:

You can write this to session data and recall it later, as one potential solution.

Modify/add in your example code block:

session_start();
if($rs){
    $_SESSION['checkData'] = '<div >Success!</div>';
}else{
    $_SESSION['checkData'] = '<div >Not working!</div>';
}

header("Location: /index.php?id={$id}");

And back on index.php, you would need to add/modify:

session_start();
if( isset( $_SESSION['checkData'] ) ){ // check whether it's set
    echo $_SESSION['checkData']; // output variable
    unset( $_SESSION['checkData']; // reset variable
}

CodePudding user response:

You can use $GLOBALS OR $_SESSION as an array for $GLOBALS more details click here

you can use it as the below code in your header file

set value in $GLOBALS

$GLOBALS['checkError'] = '<div >Success!</div>';

get value from $GLOBALS

echo $GLOBALS['checkError'];

CodePudding user response:

You have to store your data in cookies or session before the redirect. Session is also used for storing data but I suggest you to store data in cookie because It store your data on server and it create performance issue whereas cookie is store on client side. You can check key difference of Cookie and Session

I have added example for cookie (You can adjust expire time of cookie here I have set expire time for 1 day and based on your code I think you have to just blink your message on redirected page then you can set expiry time for 5s - 10s)

<?php
    $cookie_name = "user";
    $cookie_value = "John Doe";
    setcookie($cookie_name, $cookie_value, time()   (86400 * 30), "/"); // 86400 = 1 day
?>

On second page you can access that cookie

<?php
    if(!isset($_COOKIE[$cookie_name])) {
        echo "Cookie named '" . $cookie_name . "' is not set!";
    } else {
        echo "Cookie '" . $cookie_name . "' is set!<br>";
        echo "Value is: " . $_COOKIE[$cookie_name];
    }
?>

I hope this is helpful to you.

CodePudding user response:

Another way, very easy to implement is to skip $checkError variable and redirect user to a specific page:

// ...
$rs = mysqli_query($con, $sql);
mysqli_close($con);
$page = $rs ? 'success' : 'failure';
header("Location: /{$page}.php?id={$id}");
  • Related