0

Hello,

Noob alert!!

I tried to protect some pages with a code I found googling. But since my server was recently moved to a better one, the code stoped working and gives me the error "Fatal error: Call to undefined function session_is_registered() .../protect_page.php on line 3"

I googled it, and I think it's happening because of the version of PHP.
I tried to fix it, but no joy!

I would appreciate if you could help me! I will paste here the code I have.

the code I got on every page I wish to protect
<? include('protect_page.php')?>

login.php

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Log in</title>
    <link rel="stylesheet" media="all" href="pwstyle.css" />
<script>
function goBack()
  {
  window.history.back()
  }
</script>
</head>
<body>
    <form name="form1" method="post" action="checkpw.php">
        <div id="container">
            <p>
            <span id="pass">Password:</span>
            <span id="formfield"><input name="pw" type="text" id="pw"></span>
            <span class="submitbt"><input type="submit" name="submitbt"></span>
            </p>
            <p><input type="button" value="Back" onclick="goBack()"></p>
        </div>
    </form>
</body>
</html>

protect_page.php

<? 
session_start();
if(!session_is_registered(pw)){
header("location:login.php");
}
?>

checkpw.php

<?php
// pw is the password sent from the form 
$pw=$_POST['pw'];

$pw = stripslashes($pw);

// you can make this much more robust by checking against a database in this file at this point

if($pw == 'password1'){
    session_register("pw"); 
    header("location:domino.php");
}
elseif($pw == 'password2'){
    session_register("pw"); 
    header("location:domino.php");
} 
elseif($pw == 'password3'){
    session_register("pw"); 
    header("location:domino.php");
} else {
    header("location:wrong.php");
}
?>

Can you help me?

4
Contributors
6
Replies
21
Views
3 Years
Discussion Span
Last Post by pzuurveen
0

Hello thanks for the reply!
I changed my protect_page.php with if(!isset($_SESSION['pw'])), but now I have a new error: Fatal error: Call to undefined function session_register() in /home/mylemcom/public_html/#/checkpw.php on line 10.
I tried to change session_register("pw"); with $_SESSION["pw"];,it didn't give the error but nothing happened.

What can I do to fix this issue?

Thank you

0

Why session register?

Why not just: $_SESSION["pw"] = "blah"; and then you can: if(!isset($_SESSION["pw"])) { // }

etc..

0

I'm sorry, but I'm not a coder! I was just following a tutorial I found, and it worked for a couple of years, until now!

So I don't understand the code to fix it myself! I was trying to see if someone could give me some instructions to fix the existing code.

0

So.. The use of session_register is depreciated.. Instead, use:

$_SESSION['uname'] = "Phorce"; whereas before I might have used session_register("phorce"); i don't know, I never used that function.

Now, everytime you want to check if the session exisits: if(!isset($_SESSION['uname'])) { // } and to output it: print($_SESSION['uname']); I'm sorry but I don't understand what you find so difficult about it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.