将验证码保存在session,当用files来保存session的时候,用户注册的时候能在后台$_SESSION['captcha']读取到这个验证码,也就是说能正常传递,但是改为用mysql来保存session的时候,能正常储存session(在数据库里能看到有captcha的session值),在生成验证码的那个页面用$_SESSION['captcha']也是能读到那个值的,但是之后用户注册后台另一个页面却读取不到这个值,(也就是说不能传递了),很久之前都是可以的,现在发现居然不行了,有没有谁能想到是什么原因?
回复讨论(解决方案)
如果 session 用数据库保存,那么每个使用 session 的页面都必须加载相应的代码
你做到了吗?
这有个 session问题详解 可以看下 http://www.phpthinking.com/archives/318
如果 session 用数据库保存,那么每个使用 session 的页面都必须加载相应的代码
你做到了吗?
嗯嗯,每次要用session的时候都会
if (!isset($_SESSION)){
require_once __DIR__ . '/SessionHandler.php';
new \lib\SessionHandler();
}
SessionHandler.php文件里就有相应的代码,但问题还是其它页面读取不了session的值啊~
为防止意外情况出现
程序应无条件加载 SessionHandler.php
而实例化对象和 session_start() 都应在 SessionHandler.php 中进行
你还使用了命名空间,这就更需要注意排查新的故障点
为防止意外情况出现
程序应无条件加载 SessionHandler.php
而实例化对象和 session_start() 都应在 SessionHandler.php 中进行
你还使用了命名空间,这就更需要注意排查新的故障点
去掉条件引用以及去掉命名空间还是不行。
现在不说不同的页面,就算是同一个页面也读取不了,如下
error_log($_SESSION["captcha"]);
$_SESSION["captcha"] = $text;
第一句error_log($_SESSION["captcha"]);中的$_SESSION["captcha"]是想读取前一次存在数据库中的captcha 的值,此时数据库中是有值的,但是就是读不出来,显示“PHP Notice: Undefined index: captcha”的错误,
我的SessionHandler类中的session_read函数是这样的:
function readSession($SID){
$query = "select value from sessioninfo where sid= :sid";
$stmt = $this->dbLink->prepare($query);
$stmt->bindParam(':sid', $SID);
if($stmt->execute())
{
$value = $stmt->fetch(\PDO::FETCH_ASSOC);
/*error_log('sid:'.$SID);*/
/*error_log('value:'.implode(',', $value));*/
if(!empty($value))
{
return $value;
}
}
}
上面注释掉的代码读取到的$value是有值的,也就是说php读取到了值但是没有反序列化到内存里?
为防止意外情况出现
程序应无条件加载 SessionHandler.php
而实例化对象和 session_start() 都应在 SessionHandler.php 中进行
你还使用了命名空间,这就更需要注意排查新的故障点
去掉条件引用以及去掉命名空间还是不行。
现在不说不同的页面,就算是同一个页面也读取不了,如下
error_log($_SESSION["captcha"]);
$_SESSION["captcha"] = $text;
第一句error_log($_SESSION["captcha"]);中的$_SESSION["captcha"]是想读取前一次存在数据库中的captcha 的值,此时数据库中是有值的,但是就是读不出来,显示“PHP Notice: Undefined index: captcha”的错误,
我的SessionHandler类中的session_read函数是这样的:
function readSession($SID){
$query = "select value from sessioninfo where sid= :sid";
$stmt = $this->dbLink->prepare($query);
$stmt->bindParam(':sid', $SID);
if($stmt->execute())
{
$value = $stmt->fetch(\PDO::FETCH_ASSOC);
/*error_log('sid:'.$SID);*/
/*error_log('value:'.implode(',', $value));*/
if(!empty($value))
{
return $value;
}
}
}
上面注释掉的代码读取到的$value是有值的,也就是说php读取到了值但是没有反序列化到内存里?
原来问题果然是出现在这里的~
不应该return $value而是应该return $value['value'];
终于解决了~谢谢你们~
这有个 session问题详解 可以看下 http://www.phpthinking.com/archives/318
这片文章讲解得非常详细~好文章啊~
这有个 session问题详解 可以看下 http://www.phpthinking.com/archives/318
这片文章讲解得非常详细~好文章啊~ 谢谢 希望多多支持下 我的个人博客 可以做个朋友 QQ 541578455

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
