PHP and MySQL Session - php

we moved our website to a new server that came with a new IP address. What puzzles me; the website login sessions do not work on the new server but when I change the database IP to the old server they are working.
MySQL Version :
Old server = 5.1.58- Community
New server = 5.1.68 - Community
At first I thought it was a PHP error but I now believe it's not and suspect its MySQL related. Anyone who knows what might have caused this conflict?
Debugging Error :
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2 Notice: Undefined index: uUserTypeID in C:\inetpub\wwwroot\gtest\admin\index.php on line 50 Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52 Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Line 50 :
GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);
Line 52 :
if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))
Code overview :
<?php
error_reporting(E_ALL);
ini_set('display_errors', True);
session_start();
require '../libs/database.php';
require '../libs/users.php';
require '../libs/products.php';
require '../libs/quotes.php';
require '../libs/common.php';
require 'functions.admin.php';
if (!($_SESSION['uAUID']) > 0)
{
DisplayLoginForm();
}
else
{
**GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);**
**if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))**
{
if (isset($_POST['eProdID']) && isset($_POST['eProdGroupID']))
{
$_SESSION['page'] = 'edit_product';
$_SESSION['page_header'] = 'Edit Product';
}
else if (isset($_POST['eProdGroupID']))
{
$_SESSION['page'] = 'edit_product_group';
$_SESSION['page_header'] = 'Edit Product Group';
}
else if (isset($_POST['eAUID']))
{
$_SESSION['page'] = 'edit_user';
$_SESSION['page_header'] = 'Edit User';

Check over your included files/code structure.. A usual cause for this error is:
session_start();
/* Random Code here /*
session_start();
Just the duplicate lines of session_start(); So what I will suggest is to look over your included files/main page(s) that you are receiving this error message on, and check for more than one session_start();

Related

How To Start Multiple Session

Hye, I've been creating this Quiz System, and I found myself in an error...The error started when I tried to add a DDOS Protection...Both start.php and configs.php started a session..and if the include function called start.php first, then it will only run start.php session and ignored the configs.php and vice versa. My question is how do i start both session at the same time so my website can work properly? Take a look at my code.
This is configs.php
P/S : I cut of some code to keep it short
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 'On');
function rewrite_urls($change){
$match = [
'/userdetails.php\?id=([0-9]+)/',
'/userdetails.php/',
'/plans.php/',
'/statistics.php\?id=([0-9]+)&t=([A-Za-z0-9_-]+)/',
'/quizzes.php\?request=my/',
'/quizzes.php\?id=([0-9]+)&t=([A-Za-z0-9_-]+)&request=results&r=([0-9]+)/',
'/quizzes.php\?id=([0-9]+)&t=([A-Za-z0-9_-]+)&request=results/',
'/quizzes.php\?id=([0-9]+)&t=([A-Za-z0-9_-]+)/',
'/quizzes.php\?c=([0-9]+)\&t=([A-Za-z0-9_-]+)\&page=([0-9]+)/',
'/quizzes.php\?c=([0-9]+)\&t=([A-Za-z0-9_-]+)/',
'/quizzes.php\?page=([0-9]+)/',
'/quizzes.php/',
This is my start.php
P/S : I cut of some code to keep it short
<?php
/**
* AntiDDOS System
* FILE: index.php
* By Sanix Darker
*/
function safe_print($value){
$value .= "";
return strlen($value) > 1 && (strpos($value, "0") !== false) ? ltrim($value, "0") : (strlen($value) == 0 ? "0" : $value);
}
if(!isset($_SESSION)){
session_start();
}
if(isset($_SESSION['standby'])){
// There is all your configuration
$_SESSION['standby'] = $_SESSION['standby']+1;
$ad_ddos_query = 5;// ​​number of requests per second to detect DDOS attacks
$ad_check_file = 'check.txt';// file to write the current state during the monitoring
$ad_all_file = 'all_ip.txt';// temporary file
$ad_black_file = 'black_ip.txt';// will be entered into a zombie machine ip
$ad_white_file = 'white_ip.txt';// ip logged visitors
$ad_temp_file = 'ad_temp_file.txt';// ip logged visitors
$ad_dir = 'anti_ddos/files';// directory with scripts
$ad_num_query = 0;// ​​current number of requests per second from a file $check_file
$ad_sec_query = 0;// ​​second from a file $check_file
$ad_end_defense = 0;// ​​end while protecting the file $check_file
$ad_sec = date("s");// current second
$ad_date = date("is");// current time
$ad_defense_time = 100;// ddos ​​attack detection time in seconds at which stops monitoring
I also have head.php which will be called in every page
This is the code for head.php
<?php
try{
if (!file_exists('anti_ddos/start.php'))
throw new Exception ('anti_ddos/start.php does not exist');
else
require_once('anti_ddos/start.php');
}
//CATCH the exception if something goes wrong.
catch (Exception $ex) {
echo '<div style="padding:10px;color:white;position:fixed;top:0;left:0;width:100%;background:black;text-align:center;">'.
'The "AntiDDOS System" failed to load '.
'properly on this Web Site, please de-comment the \'catch Exception\' to see what happening!</div>';
//Print out the exception message.
//echo $ex->getMessage();
}
include __DIR__."/configs.php";
?>
Any Idea?

Web crawler in PHP not able to create records in mysql database. How to fix?

I am creating a web crawler for a search engine like Google. The web crawler works well, that's what I see when I run it through terminal, but it is not writing any records in the mysql database.
I have already tried granting all permissions to the database user the web crawler uses, but it was of no use. My server is flawless, that I can be sure of.
<?php
$start = "http://localhost/mariophp/test.html";
$already_crawled=array();
$crawling=array();
function get_details($url)
{
$options=array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: ZeroBot/0.2\n"));
$context=stream_context_create($options);
$doc = new DOMDocument();
#$doc->loadHTML(#file_get_contents($url,false,$context));
$title=$doc->getElementsByTagName("title");
$title=$title->item(0)->nodeValue;
$simg=$doc->getElementsByTagName("img");
//$simg=$simg->getAttribute("src");
//$simg=$simg->item(0)->nodeValue;
$description="";
$keywords="";
$metas=$doc->getElementsByTagName("meta");
for($i=0; $i<$metas->length; $i++)
{
$meta=$metas->item($i);
if($meta->getAttribute("name")==strtolower("description"))
$description=$meta->getAttribute("content");
if($meta->getAttribute("name")==strtolower("keywords"))
$keywords=$meta->getAttribute("content");
}
$_con=mysqli_connect("localhost","augustus","password");
mysqli_select_db($_con,"websited");
$title=$_POST["title"];
$url=$_POST["url"];
$keywords=$_POST["keywords"];
$description=$_POST["description"];
$simg=$_POST["simg"];
$sql="insert into websited(stitle,slink,skey,sdesc,simg) values('$title','$url',$keywords',$description','$simg')";
if(!mysqli_query($_con,$sql))
{
echo "Error: mysqli_error($_con))";
}
}
function follow_links($url)
{
global $already_crawled;
global $crawling;
$options=array('http'=>array('method'=>"GET", 'headers'=>"User-Agent: MarioBot/0.1\n"));
$context=stream_context_create($options);
$doc = new DOMDocument();
#$doc->loadHTML(#file_get_contents($url,false,$context));
$linklist = $doc->getElementsByTagName("a");
foreach ($linklist as $link)
{
$l = $link->getAttribute("href");
if(substr($l,0,1)=="/" && substr($l,0,2)!="//")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].$l;
}
else if (substr($l,0,2)=="//")
{
$l=parse_url($url)["scheme"].":".$l;
}
else if(substr($l,0,2)=="./")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].dirname(parse_url($url)["path"]).substr($l,1);
}
else if(substr($l,0,1)=="#")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"].parse_url($url)["path"].$l;
}
else if(substr($l,0,3)=="../")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"]."/".$l;
}
else if(substr($l,0,11)=="javascript:")
{
continue;
}
else if(substr($l,0,5)!="https" && substr($l,0,4)!="http")
{
$l=parse_url($url)["scheme"]."://".parse_url($url)["host"]."/".$l;
}
if(!in_array($l,$already_crawled))
{
$already_crawled[]=$l;
$crawling[]=$l;
echo get_details($l)."\n";
//echo $l."\n";
}
}
array_shift($crawling);
foreach ($crawling as $site) {
follow_links($site);
}
}
follow_links($start);
print_r($already_crawled);
?>
Note:The test.html file mentioned in the code is a simple file containing links to different websites.
You may need to setup a server first to successfully run this code.
I am getting the following output right now.
[augustoandro#Augustus zerophp]$ php crawle2.php
PHP Notice: Undefined index: title in /srv/http/zerophp/crawle2.php on line 30
PHP Notice: Undefined index: url in /srv/http/zerophp/crawle2.php on line 31
PHP Notice: Undefined index: keywords in /srv/http/zerophp/crawle2.php on line 32
PHP Notice: Undefined index: description in /srv/http/zerophp/crawle2.php on line 33
PHP Notice: Undefined index: simg in /srv/http/zerophp/crawle2.php on line 34
PHP Recoverable fatal error: Object of class mysqli could not be converted to string in /srv/http/zerophp/crawle2.php on line 39
[augustoandro#Augustoandro zerophp]$
Please help.
Get rid of these lines:
$title=$_POST["title"];
$url=$_POST["url"];
$keywords=$_POST["keywords"];
$description=$_POST["description"];
$simg=$_POST["simg"];
These are overwriting the variables that you got from scraping the website. $_POST is for getting parameters that are submitted from a form or AJAX, they're not needed here.
The call to mysqli_error() should not be inside a string. Change
if(!mysqli_query($_con,$sql))
{
echo "Error: mysqli_error($_con))";
}
to
if(!mysqli_query($_con,$sql))
{
echo "Error: " . mysqli_error($_con));
}

PHP session error A session had already been started and Undefined variable: _session

I want to create from with captcha. If captcha not correct then show same valaue in form.
This is page for add data to session. It can show data from session such as echo $_session['txtShippingFirstName']; here.
// Use captcha
if(!((md5((isset($_POST['captcha'])?$_POST['captcha']:"")) == $_SESSION['captchaKey']) || (isset($_SESSION['plaincart_customer_id'])))){
$_session['txtShippingFirstName'] = $_POST['txtShippingFirstName'];
$_session['txtShippingLastName'] = $_POST['txtShippingLastName'];
$_session['txtShippingEmail'] = $_POST['txtShippingEmail'];
$_session['txtShippingAddress1'] = $_POST['txtShippingAddress1'];
$_session['txtShippingAddress2'] = $_POST['txtShippingAddress2'];
$_session['txtShippingPhone'] = $_POST['txtShippingPhone'];
$_session['txtShippingCity'] = $_POST['txtShippingCity'];
$_session['txtShippingState'] = $_POST['txtShippingState'];
$_session['txtShippingPostalCode'] = $_POST['txtShippingPostalCode'];
//setError($_session['txtShippingFirstName']);
setError("CAPTCHA not correct");
header("Location: checkout.php?step=1");
}else{
$includeFile = 'checkoutConfirmation.php';
}
This is form page if captcha not correct then return to this page
if(isset($_session['txtShippingFirstName'])){
$customerFirstName = $_session['txtShippingFirstName'];
$customerLastName = $_session['txtShippingLastName'];
$customerEmail = $_session['txtShippingEmail'];
$customerAddress = $_session['txtShippingAddress1'];
//$_session['txtShippingAddress2']
$customerPhone = $_session['txtShippingPhone'];
$customerCity = $_session['txtShippingCity'];
$customerState = $_session['txtShippingState'];
$customerPostalCode = $_session['txtShippingPostalCode'];
}
echo $_session['txtShippingFirstName'];
It show error Undefined variable: _session on line echo $_session['txtShippingFirstName']; . If I add session_start(); on the top of page it show error Notice: A session had already been started - ignoring session_start() because I add session_start in header file already. How to fix this error ?
The PHP superglobals need to be in uppercase:
$_SESSION['txtShippingFirstName']
in this case.

how to recognise that I am able to log in to rets?

how to recognise i am able to log in to rets ?
This is my code and output.
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
//date_default_timezone_set('America/New_York');
require_once("vendor/autoload.php");
//$log = new \Monolog\Logger('PHRETS');
//$log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
$config = new \PHRETS\Configuration;
$config->setLoginUrl('*****************');
$config->setUsername('****');
$config->setPassword('****');
$config->setUserAgent('****');
$config->setRetsVersion('1.7.2');
$config->setOption('disable_follow_location',false);
$config->setOption('use_post_method',true);
$rets = new \PHRETS\Session($config);
$rets->setLogger($log);
$connect = $rets->Login();
var_dump($connect->getBody());
output
Notice:
Trying to get property of non-object in
/var/www/html/glvar/rets/vendor/troydavisson/phrets/src/Parsers/GetMetadata/System.php
on line 26
Fatal error:
Call to a member function attributes() on a non-object in
/var/www/html/glvar/rets/vendor/troydavisson/phrets/src/Parsers/GetMetadata/System.php
on line 26
how can i know from this response that i am login in to rets and
i can get any data.
$config = new \PHRETS\Configuration;
$config ->setLoginUrl(***)
->setUsername(***)
->setPassword(***)
->setRetsVersion(***)
->setOption('use_post_method', false)
->setOption('disable_follow_location', false);
empty($user_agent') ? '' : $config->setUserAgent($user_agent);
empty($user_agent_password) ? '' : $config->setUserAgentPassword($user_agent_password);
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
if ($connect) {
echo " + Connected\n\n";
return $connect;
} else {
echo" + NotConnected\n\n";
}
Here is an ever better way to make sure.. add the code below you $connect and you should get something like the below.. and any response not an error should indicate a connection and show you what was returned..
$connect = $rets->Login();
print "<pre>";
print_r($connect);
print "</pre>";
This will be returned or some thing similar if you are connected..
PHRETS\Models\Bulletin Object
array {
[body:protected] =>
}
Watch out on the above answer.. I ran it on a live RETS feed and it stopped my program in its tracks.. and printed + Connected... I removed the return $connect and changed the \n to and it ran fine and printed Connected.. but my point is it may appear to work fine but the program will not go past the connection test... so it killed my program dead in its tracks... not sure why?

I am receving an undefined error on a $_POST method [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
This is strange but I am posting an input using the $_POST method which is below:
$c = count($_POST['gridValues']);
But the problem is that I am receiving an error stating:
Notice: Undefined index: gridValues in /web/stud/..../ on line 40
(which is line above)
How come I am receiving this error because the $_POST method is definitly correct?
Below is the whole code:
<?php
ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_divisor', '1');
ini_set('session.gc_probability', '1');
ini_set('session.cookie_lifetime', '0');
require_once 'init.php';
ini_set('display_errors',1);
error_reporting(E_ALL);
session_start();
?>
$i = 0;
$c = count($_POST['gridValues']);
for($i = 0; $i < $c; $i++ ){
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
case "5":
$selected_option = "A-E";
break;
}
}
You need to check if it is set first:
<?php
if(isset($_POST['gridValues'])) {
$c = count($_POST['gridValues']);
}
?>
There are a few ways to prevent a notice level message about an undefined variable.
Put # in front of the variable. ex: $c = #$_POST['gridValues'] This can be dangerous as you are explicitly telling it to ignore any problems with the variable. See the PHP manual for more info on #: http://php.net/manual/en/language.operators.errorcontrol.php
Turn off (or change) error reporting. ex: ini_set('display_errors', 0);
Check if the variable is defined before using it. ex:
$c = '';
if(!empty($_POST['gridValues'])){
$c = count($_POST['gridValues']);
}

Categories