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
Related
I'm a beginner of PHP coding which I face this problem and I tried to fix it.
I have search through stackoverflow for answers but it stills no good.
This is my Login form.php file
<form name = 'LoginForm' method = 'POST' action = 'verifyUser.php'>
<br />
E-MAIL: <input type = "Textbox" Name = "App_Email"><br><br>
PASSWORD: <input type = "password" Name = "App_Password"><br><br>
<input type = 'Submit' name = 'Login' value = 'Log in'><br><br>
</form>
This form will goes to verifyUser.php and these are codes
include ('DBconnect.php');
$username = $_POST['App_Email'];
$pass = $_POST['App_Password'];
if($username=='' || $pass=='') {
header("Location:login.php?id=Some fields are empty");
}
$result = mysql_query("SELECT * FROM applicant_acct ");
if(!$result) {
die("Query Failed: ". mysql_error());
} else {
$row = mysql_fetch_array($result);
if ($username==$row['App_Email']) {
if($username==$row['App_Email'] && $pass==$row['App_Password']) {
header("Location: index.html?id=$username");
} else {
header("Location:login.php?id=username or your password is incorrect. Please try again");
}
}
}
And final DBconnect.php
<?
$dbc = mysql_connect('localhost','root','root') OR die('Wrong Connection!!!!!!!');
mysql_select_db('onlinerecruitment') OR die ('Cannot connect to DB.');
?>
I really have no idea why it shows "Query Failed: No database selected"
I think the problem is in verifyUser.php but have no idea where.
And another thing, after I logged in how can I generate the text "Welcome - "Username"" and provide them the logout button?
Please help.
Thank you.
Generally you may want to research a graphical user interface such as XAMPP or MySQL workbench until you are more comfortable with Database systems.
Here it seems like most of the improvements can be made in you DBConnect.php file. You are beginning and I can appreciate that. Consider something along the following lines that incorporates additional the security of PDO:: static calls.
<?php
public function _dbconnect($hostpath, $database, $username, $password){
try {
$this->conn = new PDO("mysql:host = {$hostpath};
dbname - {$database};
charset = utf8",
$username,
$password);
} else { exit(); }
?>
If this particular code block doesn't help I would highly recommend that you continue by investigating PDO:: calls.
<?php
include ('DBconnect.php');
if(isset($_POST['Login'])){
$username = $_POST['App_Email'];
$pass = $_POST['App_Password'];
if(empty($username) || empty($pass) || ctype_space($username) || ctype_space($pass)){
header("Location:login.php?error=1");
} else {
$result = mysql_query("SELECT * FROM applicant_acct");
if(!$result) {
die("Query Failed: ". mysql_error());
} else {
$row = mysql_fetch_array($result);
if($username==$row['App_Email'] && $pass==$row['App_Password']) {
header("Location: index.php?id=$username");
} else {
header("Location:login.php?error=0");
}
}
?>
I have a lot to say about your code.
Use isset function . This function check if something was done.
Check your database details again. Maybe you wrote something
wrong (misclick or something)
Use $_GET['error'] to get errors. I set 1 = for empty characters and 0 for 0 match between database and inputs.
Use sessions for after login message. You can also use Session to handle your errors.
EDIT: I recommend you to start to learn MySQLi or PDO.
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.
guys. I would like to make this script, or other with the same effect:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
To create databases with different names, for example:
First time will create database named "news1", the next time, the script will just do sth. like that: news1+1 = news2, and will create database called news2, and so on...
I hope you got my point and I'll really appreciate it if you help me to make this...
Regards, Denis Saidov.
To do this I'm guessing that you don't just want to loop 100 times?
You will need to save a variable to a text file then call it again.
Writing $text to a file:
$var_str = var_export($text, true);
$var = "<?php\n\n\$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
Retrieving it again:
include 'filename.php';
echo $text;
All together:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_number = get_var();
$my_db = 'news' . $db_number;
if (mysql_query("CREATE DATABASE ".$my_db,$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
$new_db_number = $db_number + 1;
save_var($new_db_number);
mysql_close($con);
function save_var($var) {
$var_str = var_export($text, true);
$var = "<?php\n\n\$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
}
function get_var()
include 'filename.php';
return $text;
}
?>
PS I think that should work only typing not tested
The database should be static. It is a one off event to create the database along with:
Tables
Indexes
Various constraints
Triggers
Stored procedures
This can be done with a SQL script.
Then the PHP stuff accessing this database only needs to use select, insert, delete or update statements - or as I prefer using stored procedures.
You are going to have a lot of headaches having multiple databases. Also I would hate to have to write a PHP script to do that each time and to administrate them.
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()
I am creating a ajax notification and this is part of my system allowing a user to favorite, or archive, that notification. The problem is that this php code below won't work and there is no error in the queries because the or die returns nothing. What is returned is just error. That is all it is echoing. I know the javascript is correct and sending the correct information because I have checked the network tab to see. Are there any major errors that I am missing?
<?php
require_once('.conf.php');
$notid = mysql_real_escape_string($get['notification_id']);
$username = mysql_real_escape_string($_SESSION['uname']);
$action = mysql_real_escape_string($get['action']);
if ($action == 'add') {
$insert = mysql_query("UPDATE updates SET object_fav = 1 WHERE username = '$username' AND id = '$notid'") or die('Could not connect: ' . mysql_error());
echo 'success';
} elseif($action == 'sub') {
$remove = mysql_query("UPDATE updates SET object_fav = 0 WHERE username = '$username' AND id = '$notid'") or die('Could not connect: ' . mysql_error());
echo 'remove';
} else {
echo 'error';
}
?>
PHP has no default array called $get. Perhaps you intend to use the $_GET superglobal.
$action = mysql_real_escape_string($_GET['action']);
$notid = mysql_real_escape_string($_GET['notification_id']);
It prints error when $action is not matched in your if/else chain, because the variable isn't correctly set.
Be sure that you are developing with display_errors turned on, and error_reporting(E_ALL);. The undefined variable $get would display warnings on screen.
ini_set('display_errors', 1);
error_reporting(E_ALL);