PrestaShop Fatal error in function isLogged() - php

Well, im developing a CMS based form with a copy-center-system (this is the form), but i need the $cookie->id_customer.
But i get an error:
Fatal error: Call to a member function isLogged() on a non-object in /home/papelari/public_html/modules/mymodule/submit.php on line 13
<?php
/*
$con = mysql_connect("localhost","papelari","509494218");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("papelari_Veloso", $con); */
global $cookie;
if(!$cookie->isLogged())
{
echo 'Please login';
exit;
}
else
{
echo 'Hi, ' . $cookie->customer_firstname. ' ' .$cookie->customer_lastname . '<br/>';
echo 'We contact to you: '. $cookie->email;
}
/* $sql="INSERT INTO ps_copias (id_customer, file, cor, copias, pags, papel, gramagem, flag) VALUES('$cookie- >id_customer','$_POST[file]','$_POST[cor]','$_POST[copias]','$_POST[pags]','$_POST[papel]','$_POST[gramagem]', 0)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Enviado com sucesso!";
mysql_close($con); */
?>

Your problem is that $cookie is not defined. Paste this at the beginning of your script :
<?php
include("../config/config.inc.php");
$cookie = new Cookie("ps");
if ($cookie->isLogged()) {
// your code
}
You may have to adapt config.inc.php include path depending on your own script

I am not sure that the global $cookie is for.
Make sure that $cookie is defined by using var_dump($cookie); to make sure the variable is what you think it is (and contains the correct functions).

For prestashop 1.5, to check is user is logged or not :
Context::getContext()>customer>isLogged()

Related

can anyone please tell me whats wrong with my session?

I'm trying to add an announcement with the need of the user [ id] who posted it and I was going to take it via session I don't know its not working I keep getting this error (Fatal error: Cannot redeclare check_login() (previously declared in C:\xamppp2\htdocs\session.php:17) )
and I'm trying to insert current time and date separately is (date(),now()) are the right functions? I'm trying to insert those too, could you guys possibly help? here's my code
I included the session on top of my page
<?PHP include('session.php');?>
and here's the rest of my php file
<?PHP
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
if(!empty($_POST))
{
$msg = '';
$error = '';
$title = $_POST['title'];
$desc = $_POST['desc'];
if(trim($title) == '')
{$error = 'Please enter event title';}
else if(trim($desc) == '')
{$error = 'Please enter description ';}
$IID =$_SESSION['userid'];
if($error=='') {
$qry6="INSERT INTO announcement (`Atitle`,`Adescription`,`Adate`,`Atime`,`IID`) VALUES
('$title','$desc',date(),now(),$IID)" ;
$result6=mysql_query($qry6);
$msg = " Announcement is added ";
}
}
mysql_close($link);
?>
and here's my actual session script..
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
ob_start();
session_start();
function check_login(){
if(!empty($_SESSION['userid'])){
return 1;
}else{
return 0;
}
}
?>
This isn't a session problem. You are as your error message says trying to redeclare check_login().
You can only declare a function once.
So depending on how your files are structured you need to make sure that that function is only declared once. Perhaps you use includes in your files. You either need to make sure it isn't redeclared so think about how you structure it so that doesn't happen or use require_once, this adds more overhead to your script though.
http://php.net/manual/en/function.require-once.php

PHP: Function no query & return

I hope that someone sharp on PHP can help me with problem, that i really don't understand.
I have 2 scripts. 1: test.php 2:functions.php.
I created a little test where i called a functions in functions.php frim test.php and it work fine. I got a return and it was as expected. I also have a third script register.php where i have a query to a database and that work fine.
So I wanted the query to work as a function written in functions.php
Problem: It seems that it won't make the database query! But there is createt a connection
If I move the exactly same query to test.php, it works! Is there some kind of speciel reason for this? I quit new to php, but knows a little about Java, C, JavaScript, Python.
I have checked that my include / require is all in order.
1: test.php:
<?php
require 'includes/functions.php';
$name = 'testuser';
$_ok = 0;
$_ok = check_username ($name);
printf ( 'Navn i database?: ' . $_ok . '<br>' );
?>
2: functions.php:
<?php
require 'includes/db_connect.php';
// Check connection
if (! $mysqli) {
die ( 'mysqli_init_failed' );
}
if (mysqli_connect_errno ()) {
die ( 'Failed to connect to the Database, Sorry..! errorcode: ' .
mysqli_connect_errno() . ' <br>' . 'Error Type: ' . mysqli_connect_error () );
}
if ($debug_mode_c) {
print ('Connection Established: ' . mysqli_get_host_info ( $mysqli ) . '<br>') ;
print ('session_id: ' . session_id ()) . '<br>';
}
// Set HTML-Header to utf-8.
header ( 'Content Type: text/html; charset=UTF-8' );
// Functions
function check_username($_ok) {
$name = 'testuser';
$prep_stmt = "SELECT username FROM customs WHERE username=? LIMIT 1 ";
$stmt = mysqli_prepare($mysqli, $prep_stmt);
mysqli_stmt_bind_param($stmt, 's', $name);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $name_db);
mysqli_stmt_fetch($stmt);
if ($name == $name_db) {
echo "hello";
mysqli_close ($stmt);
$_ok = 0;
} else {
$name = '';
$_ok = 2;
}
mysqli_free_result($stmt);
mysqli_close($stmt);
return $_ok;
}
Maybe found the reason for no query.
Apparently the include script containing establish connection, is not loaded as the function is called in functions.php.
When the query code is in test.php, that include functions.php, all code is read, also the connection for the database.
But even if include 'db_connect' is inside function, it won't work !?! :-(
There is nothing like noquery() function in PHP. please just check it in the include file of the database connection. you will find a user defined function in your include file of the database connection.

Multiple PHP "header(url)" in script, "Cannot modify header information" Error

Hey I have some script that is supposed to change the url of the page if it meets certain criteria, or somewhere else if it doesn't.
I'm getting this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/hermes/bosweb25c/b1401/ipg.website/process.php:7) in
/hermes/bosweb25c/b1401/ipg.website/process.php on line 53
Here's my PHP. I'm pretty sure it has to do with the if statement, but it was working fine on my WAMP Server before I uploaded to my web host.
<?php
session_start();
$con = mysql_connect('website', 'members_db_admin', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
// make members the current db
$db_selected = mysql_select_db('members_db', $con);
if (!$db_selected) {
die ('Can\'t use members database : ' . mysql_error());
}
$hash_password = md5($_POST['password']);
$email = $_POST['email'];
$result = mysql_query("SELECT email,password FROM `members` WHERE email = '$email'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
}
$row = mysql_fetch_row($result);
if ($row[0] != $email && $hash_password != $row[1])
{
$query = "INSERT INTO members (email, password)
VALUES('".$_POST['email']."','".$hash_password."')";
// Perform Query
$result2 = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result2) {
$message = 'Invalid query: ' . mysql_error() . "\n";
//$message .= 'Whole query: ' . $query;
die($message);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$_SESSION['loggedin']="YES";
$url = "Location: /welcome.php";
header($url);
}
$_SESSION['email']=$_POST['email'];
$_SESSION['password']=$hash_password;
$url = "Location: /checklogin.php";
header($url);
?>
Your problem is echo 'Connected successfully<br />';. You can't print anything out before setting headers with a header() call. Make sure you don't have any echo calls before your header().
try ob_start(); after session_start();
If you use utf-8 as encoding for this file try to save it without BOM which can cause such problems.

php mssql_connect not working?

I'm trying to access to my mssql server by php here is the code:
<?
mssql_connect($core['localhost'],$core['sa'],$core['mypass']);
mssql_select_db($core['dbone']);
?>
This is at the top of my page before <!DOCTYPE> Basically when I open my index.php no content is shown - it's like it can't connect to the mssql? Is there something i have to enable before I can access mssql_connect()?
edit: extension=php_mssql.dll is enabled if this is the right extension?
It's not "mssql" on both functions change them to "mysql" and try again.
For debugging the problem, put this in your index.php and see what gets printed out
<?php
$con = mssql_connect($core['localhost'],$core['sa'],$core['mypass']);
if(! $con) { echo 'Could not connect'; }
else { echo 'Connection successful'; }
echo '<br />';
$db = mssql_select_db($core['dbone']);
if (! $db) { echo 'Could not select database'; }
else { echo 'Database selected'; }
?>
<!DOCTYPE html>
.
.
.
Something should get printed out now.

Redirect PHP after script is run

I have a script that submits emails entered into a form to a database and I am wondering if there is a way to redirect the PHP script after the email has been submitted?
my script is -
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
please could someone give clear instructions as I am learning PHP still.
To take this redirect one step further. The header "location" should be used with a 302 response code instead of a classical 200 response code. Thios response code is 'global' to the HTTP response.
302 means 'redirect'. Most browser handle the location header even without the 302 response code, but you should set it (it could help for example ajax handling of your response).
header("Location: /foo.php",TRUE,302);
Now one step more. In fact a 303 response code (see-other) is a better redirect response code. It's called as well the redirect after post. This special redirect means for your browser that it's not really a redirect because the page he asked whas in the wrong place, but that it's really a redirect because after your POST action you need a new page result.
header("Location: /foo.php",TRUE,303);
Edit: as stated by thirtydot, a better usage is absolute url in the redirect
header("Location: http://www.example.com/foo.php",TRUE,303);
Remember that POST implies potential data changes (that GET request should'nt imply). A POST followed by a 303 is the right way to go, and will prevent your BACK button reposting the same request.
Use:
$url = 'http://your.redirect.url';
header('Location: ' . $url);
Try this:
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email) VALUES ('$_POST[email]'')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
mysql_close($con);
header('Location: http://www.example.com/');
This code taken from the header() manual page will redirect to another page on the same domain and in the same directory:
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
header("Location http://www.example.com");
That will redirect the browser to example.com, note that you can only use this if output has not already been sent to the browser, e.g make sure you have echo, print or var_dump statements before you use header()
Add send header function
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
mysql_close($con)
?>
Or send javascripts with code
<?php
$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("emails", $con);
$sql="INSERT INTO mlist (email)
VALUES
('$_POST[email]'')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}else{
echo "<script>";
echo "window.location.href= '".$_SERVER["HTTP_REFERER"]."';";
echo "</script>";
}
mysql_close($con)
?>
Redirect in the controller.
header('Location: http://www.yoursite.com/new_page.html');

Categories