Fatal error: Call to undefined function session_register() in C:\xampp\htdocs\4\admin\checklogin.php on line 33

<?php
error_reporting(0);
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="phplogin"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 


// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password"); 
header("location:index.php");
}
else {
echo ( " <script language='JavaScript'>  
             if(confirm('Error logging in: The Username & Password does not match.'))
             window.location = 'login.php';
        </script>
      ");
}
?>

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

@godslove

Fatal error: Call to undefined function session_register() in C:\xampp\htdocs\4\admin\checklogin.php on line 33

You can't used session_register() because it's outdated

Read this:

http://php.net/manual/en/function.session-register.php

If you are using PHP 5.4.0 then this function is no longer available.

Please update to PHP 5.4.7

If you want still want to used that

Then do this : $_SESSION['username'] = '$username';

My advice right now learn the current code because you will get alot of errors later on and it's getting harder for you to make the code work.

commented: To Rectify what some retard did to LastMitch +0

@LastMitch

Thank you, thank you, thank you!

Am currently testing Ubuntu 12.10 as my new develop environment. I found some on advice online suggesting one check logs with this particular error (cat /var/log/apache2/error.log). Accordingly; I searched on the PHP Fatal error found in these logs & ended up here. I guess it is to be expected that certain functions etc get deprecated with time, but it can be a hassle finding out the hard way! Once again thanks!

Member Avatar for diafol

Before assigning any session variables like $_SESSION['username'] = ...
ensure that you have session_start(); at the top of every page.

@diafol

I'm not sure if you are replying to me, but if you are firstly, thank you and rest assured I have session_start(); at the top of pages where required. My problem was just getting caught out by the deprecation of session_register();. Everthing is cool now.

Member Avatar for diafol

Sure, no problem. As it wasn't in your original code, I thought I'd add it 'just in case'.

Everthing is cool now.

:)

but now how do you check if the session is created?
Example:
on the other pages

    <?php
    session_start();
 //This dosn't work :S
   if(!session_is_registered($myusername)){
    header("location:main_login.php");
    }
    ?> 
Member Avatar for Zagga

Hi joshf97,

You set a session variable with:

$_SESSION['myusername'] = "Zagga";

you can check if it's set with:

if(!$_SESSION['myusername']){
...

See the PHP Manual for more info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.