-1

I have a problem in my login validation in PHP. This code is running fine from last two months. But today it display fatal error plz any one help me.

<?php
session_start();
if(!session_register(myusername)){
header("location:login.html");
}

Fatal error: Call to undefined function session_register()

here is the full code plz help me

<?php

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // 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 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$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("myusername");
session_register("mypassword"); 
    header("location:operator.php");
    }
    else {
    header("location: http://");

    }
    ?>

@cmorrissey

  • 4
    `session_register()` is deprecated as of 5.3 and removed as of 5.4, I know this one (*almost*) by heart now. – Funk Forty Niner Jun 18 '14 at 20:33
  • What version of PHP are you using. According to the [docs](http://www.php.net//manual/en/function.session-register.php): _"**Warning**: This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0."_ – War10ck Jun 18 '14 at 20:34
  • As Fred mentions... most likely the host upgraded PHP and you need to fix your old code. – Mattt Jun 18 '14 at 20:34
  • Did you bother searing for `session_register` on stackoverflow. @Fred-ii- I feel your pain. The [results](http://stackoverflow.com/search?q=session_register) of a simple search and the amount of duplicates are extraordinary... – War10ck Jun 18 '14 at 20:35
  • my php version is now 5.4.29 so what should i can do now – user3532635 Jun 18 '14 at 20:37
  • 1
    ...now, you go here => http://www.php.net/manual/en/session.examples.php – Funk Forty Niner Jun 18 '14 at 20:39

2 Answers2

1

I'm assuming you have upgraded to PHP 5.4.0+

You can replace this with

session_start();

if(!isset($_SESSION['myusername'])){
    header("location:login.html");
}
cmorrissey
  • 8,348
  • 2
  • 18
  • 27
  • its working but not completely. What should be for if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:http://"); } else { header("location: http://"); } – user3532635 Jun 18 '14 at 20:46
  • @user3532635 please post your complete code in a new question – cmorrissey Jun 18 '14 at 20:48
0

Has your server been upgraded?

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

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

Matt
  • 1,031
  • 14
  • 26
  • yes my server has been upgraded – user3532635 Jun 18 '14 at 20:47
  • If you can login on it via ssh and run "php -v" it will show you the php version. If it is higher then 5.3 your sollution will not work anymore and you should use cmorrissey's sollution. Check back if that doesn't work – Matt Jun 18 '14 at 20:49