Qeeet
Qeeet

Reputation: 313

php sessions doesn't work with form when submit button is clicked

I'm having an exam soon. I'm stuck with sessions. At most cases it does not work to me. No matter what I have tried. It worked for me once, but after refreshing a page I never saw it worked. My samples:

qwer.php:

<?php
session_name('Private'); 
session_id('TEST');
if(isset($_POST["Submit"])){
session_start();
$_SESSION['counter']=5;
$_SESSION["lolvalue"]=isset($_POST["imie"]);
}
?>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body>
    <form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
    <input type="text" name="imie"><br>
    <input type="submit" name="Submit">
    </form>
    <a href='qwew.php'>Link to protected file</a>
    </body>
    </html>

<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>

qwew.php

<?php
session_name('Private');
session_id('TEST');
session_start();

echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];

session_destroy();

?>

The thing is that form and redirecting to another page should be done by clicking on submit. Please help me to figure out where I am wrong. Also I forgot to mension I'm using XAMPP, session.use_cookies and session.use_trans_sid are enabled

Upvotes: 1

Views: 775

Answers (3)

Tintu C Raju
Tintu C Raju

Reputation: 2700

qwer.php

<?php
    session_start();
    if(isset($_POST["Submit"])) {
        $_SESSION['counter']=5;
        $_SESSION["lolvalue"]=isset($_POST["imie"]);
        header('Location:qwew.php');
        die(); 
    }
?>

<html>
<head>
<title>Login</title>
</head>
<body>
<form name="myform" method="post" action="qwer.php" onsubmit="return validateform();">
    <input type="text" name="imie"><br>
    <input type="submit" name="Submit">
</form>
<a href='qwew.php'>Link to protected file</a>
</body>
</html>

qwew.php

session_start();
if(isset($_SESSION["lolvalue"]) && isset($_SESSION["counter"]))
 echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
else {
   header('Location:qwer.php');
   die(); 
 }

Upvotes: 1

Prashant M Bhavsar
Prashant M Bhavsar

Reputation: 1164

qwew.php

<?php
session_start();
if(isset($_POST["Submit"])){
    $_SESSION['counter']=5;
    $_SESSION["lolvalue"]=isset($_POST["imie"]);

    echo "here it is: ".$_SESSION["lolvalue"]." and ".$_SESSION["counter"];
}
?>
    <html>
    <head>
    <title>Login</title>
    </head>
    <body>
    <form name="myform" method="post" action="qwew.php" onsubmit="return validateform();">
        <input type="text" name="imie"><br>
        <input type="submit" name="Submit">
    </form>
    <a href='qwew.php'>Link to protected file</a>
    </body>
    </html>

<script type="text/javascript">function validateform(){
checkit=/^[A-Z]{1}[a-zA-Z0-9]{7,16}$/;
if(!(myform.imie.value.match(checkit))){alert("blabla...bla");
return false;}
}
</script>

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

This code will never be executed:

if(isset($_POST["Submit"])){
    session_start();
    $_SESSION['counter']=5;
    $_SESSION["lolvalue"]=isset($_POST["imie"]);
}

That's because your HTML form is sending POST request to qwew.php file not to qwer.php.

Upvotes: 1

Related Questions