call to undefined function msg() - php

i m getting an error in the php code..
Fatal error: Call to undefined function msg()
but the function is already defined in the code here
whenever i click on a login button this login script runs
<?php
mysql_select_db("elunika", $con);
$a = $_POST['em'];
$b = $_POST['pwd'];
$c = $_POST['log'];
$b = md5($b);
if (isset($c)) {
$q = mysql_query("select * from registeration where email='$a' and password='$b'");
$r = mysql_num_rows($q);
if ($r) {
$_SESSION["Authenticated"] = 1;
$_SESSION['id'] = $a;
}
else {
$_SESSION["Authenticated"] = 0;
}
if ($_SESSION["Authenticated"] === 0) {
die(msg(0, "Incorrect Information"));
}
else {
session_write_close();
echo msg(1, "profile.php");
}
function msg($status, $txt)
{
return '{"status":' . $status . ',"txt":"' . $txt . '"}';
}
}
?>

Only unscoped functions are defined at compile-time (prior to execution). Your msg function however is just going to be defined if the if branch (the if (isset(…)) on line 9) is entered; so it's only going to be defined at the moment where the executor reaches it.
But in your code msg() is already called before the function declaration was encountered at run-time. Moving the function declaration up (= before the msg() call) should help:
function msg ($status, $txt) {
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
if($_SESSION["Authenticated"] === 0) {
die(msg(0,"Incorrect Information"));
} else {
session_write_close();
echo msg(1,"profile.php");
}

Related

Codeigniter blank page - Severity: Compile Error

I have codeigniter application which working well in justhost. I am trying to transfer it to bigrock host. But in new host its not working I can only see blank page with 500 error,no error logs in cpanel. Followed many solution in stackoverflow but couldn't fix(innitioally i thought its .htaccess problem), and now I tried define environment variable to development, I can see php error which says as below
A PHP Error was encountered
Severity: Compile Error
Message: Can't use method return value in write context
Filename: models/Home_model.php
Line Number: 77
Backtrace:
Home model screen shot
login function which is in home model:
public function login($data) {
if(filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$res = $data['email'];
}
else {
$settings = get_setting();
$pre = $settings->id_prefix;
$res = trim($data['email'],$pre);
}
$this->db->select('*');
$this->db->from('users');
$this->db->join('profiles', 'profiles.email = users.email','left');
$this->db->where('user_status','1');
$this->db->where('users.email', $res);
$this->db->or_where('matrimony_id',$res);
$chk_qry = $this->db->get();
//$chk_qry = $query->row();
//echo $this->db->last_query();die;
if ($chk_qry->num_rows() == 1) {
$pass = $this->encrypt->decode($chk_qry->row()->password);
if($data['password'] == $pass) {
$this->db->where('user_id', $chk_qry->row()->user_id);
$usr_qry = $this->db->get('profiles');
if(!empty($usr_qry->result())) {
if($usr_qry->result()[0]->profile_status != 2) {
if($usr_qry->result()[0]->profile_status != 4) {
if($usr_qry->result()[0]->profile_approval == 1) {
$status = 1;
$this->session->set_userdata('logged_in',$usr_qry->result()[0]);
$data1['date_time'] = date('Y-m-d H:i:s', time());
//$data1['user_id']=$usr_qry->result()[0]->user_id;
$data1['matrimony_id']=$usr_qry->result()[0]->matrimony_id;
$query = $this->db->where('matrimony_id',$data1['matrimony_id']);
$query = $this->db->get('active_members');
if ($query->num_rows() > 0){
$this->db->where('matrimony_id',$data1['matrimony_id']);
$this->db->update('active_members',$data1);
} else {
$this->db->insert('active_members', $data1);
}
} else { $status = 2; } // Profile Not Approved
} else { $status = 7; } // Profile Deactivated
} else { $status = 5; } // Profile Deleted or Banned
} else { $status = 6; } // No Accounts Found
} else { $status = 3; } // Ivalid Password
} else { $status = 4; } // Email not exist
return $status;
}
I wonder how this same script working fine in justhost.
Solved problem!!! silly me,
Updated php version from 5.4 to 5.6

how to access function variable outside if statement php

How can i access function variable out side if else statement as you can see when i declared opType == "Anand" than code will run but i use post man to saw result
if (!(empty($_POST)) & isset($_POST)) {
$opType = $_POST["opType"];
if ($opType == "insertFuelEngineMap") {
//insert into fuel_engine_capacity_mapping setValuesForCreation(false);
$query = "INSERT INTO fuel_engine_capacity_mapping(cf_mapping_id,capacity_id) VALUES ('$cf_mapping_id','$capacity_id')";
$loginResult = mysqli_query($link, $query);
if (mysqli_affected_rows($link) > 0) {
$userdata = array('status' => '200', 'msg' => "insertlol into fuel_engine_capacity_mapping successfully", 'mapping_id' => $link->insert_id);
} else {
$userdata = array('status' => '404', 'msg' => " Cant fuel_engine_capacity_mapping " . mysqli_error($link));
}
}
if ($opType == "anand") {
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
}
}
addition();
echo $z;
You can return the value :
//Modify you function so it will return the result
function addition() {
return $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
Then :
//Catch variable and echo
echo $z = addition();
I hope it's what you want, because your question is not clear.
Moreover, you 'll have error if $opType is different than anand because function 'll be called but it 'll not exist.
It should be better to declare it in any case and not only if $opType === 'anand'.
Declaring a function inside and if statement, only if some variable is set and then calling it outside that if statement where you do not check that that variable is set a plan for disaster!
Instead define the function in the main body of the code and call it with parameters, and just return the computed value
<?php
function addition($a, $b) {
return $a + $b;
}
$z = addition($x, $y);
echo $z;

PHP: Fatal Error: Call to a member function winner() on null

I'm getting the following errors and do not know how to proceed.
Warning: Creating default object from empty value in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
32
Notice: Undefined variable: game­ in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
17
Fatal error: Call to a member function winner() on null in
/Applications/XAMPP/xamppfiles/htdocs/ACIT4850_lab1/index.php on line
17
Here is what I have
<body>
<?php
// put your code here
$squares = $_GET['board'];
$game = new Game($squares);
if ($game­->winner('x')) // line 17
echo 'You win. Lucky guesses!';
else if ($game­->winner('o'))
echo 'I win. Muahahahaha';
else
echo 'No winner yet, but you are losing.';
?>
</body>
and
<?php
class Game {
var $position = '---------';
function __construct($squares) {
$this­->position = str_split($squares); // line 32
}
function winner($token) {
$won = false;
$result = false;
for($row=0; $row<3; $row++) {
if (($this->$position[3*$row] == $token) &&
($this->$position[3*$row+1]== $token) &&
($this->$position[3*$row+2] == $token))
$result = true;
}
...
if ($result == true)
$won = true;
return $won;
}
}?>
any help would be appreciated.
the error is because you have a strange char in this line
$this­->position = str_split($squares); // line 32
the character is \u00ad
and you have all over your code like: $something\u00ad->
try with this line of code
$this->position = str_split($squares); // line 32
aside from this you have an error in this part of the code
for($row=0; $row<3; $row++) {
if (($this->$position[3*$row] == $token) &&
($this->$position[3*$row+1]== $token) &&
($this->$position[3*$row+2] == $token))
$result = true;
}
you are using
$this->$position
and you need to use
$this->position

if else if condition not working properly

Test.php
<?php
$a = 'D:/mydomain/Slim/Lib/Table.php';
$b = '\Slim\Lib\Table';
foreach (array($a, $b) as $value)
{
if (file_exists($value))
{
echo "file_exist";
include_once($value);
new Table();
}
else if (class_exists($value))
{
echo "class_exist";
$class = new $value();
}
else
{
echo "error";
}
}
?>
and D:/mydomain/Slim/Lib/Table.php
<?php
class Table {
function hello()
{
echo "test";
}
function justTest()
{
echo "just test";
}
}
?>
When im execute test.php in browser the output result is:
file_exist
Fatal error: Cannot redeclare class Table in D:/mydomain/Slim/Lib/Table.php on line 2
if statement for class_exist is not trigger. namespace \Slim\Lib\Table is never exist.
The second, optional, parameter of class_exists is bool $autoload = true, so it tries to autoload this class. Try to change this call to class_exists( $value, false) See the manual.
The first if can be changed to:
if(!class_exists($value) && file_exists($file)
actually there are other problems:
$a = 'D:/mydomain/Slim/Lib/Table.php';
$b = 'Table'; //Since you don't have a namespace in the Table class...
//This ensures that the class and table are a pair and not checked twice
foreach (array($a=>$b) as $file=>$value)
{
if (!class_exists($value) && file_exists($file))
{
echo "file_exist";
include_once($file);
$class = new $value();
}
else if (class_exists($value))
{
echo "class_exist";
$class = new $value();
}
else
{
echo "error";
}
}

Is there any possibility to break a loop while calling a function?

Let's say I have a simple code:
while(1) {
myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) break;
}
This will not work with error code 'Fatal error: Cannot break/continue 1 level on line'.
So is there any possibility to terminate the loop during a subfunctin execution?
Make the loop condition depend upon the return value of the function:
$continue = true;
while( $continue) {
$continue = myend();
}
Then, change your function to be something like:
function myend() {
echo rand(0,10);
echo "<br>";
return (rand(0,10) < 3) ? false : true;
}
There isn't. Not should there be; if your function is called somewhere where you're not in a loop, your code will stop dead. In the example above, your calling code should check the return of the function and then decide whether to stop looping itself. For example:
while(1) {
if (myend())
break;
}
function myend() {
echo rand(0,10);
echo "<br>";
return rand(0,10) < 3;
}
Use:
$cond = true;
while($cond) {
$cond = myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) return false;
}

Categories