简体   繁体   中英

session_start() causes error: No such file or directory (fresh, portable XAMPP)

I have the following PHP code:

include './globallyUsedFunctions/connectToDatabase.php';
include './globallyUsedFunctions/hashInput.php';
session_start();

For some reason, it causes this error:

<br />
<b>Warning</b>:  session_start(): open(\xampp\tmp\sess_4p70knkr6lb7r9ha0pitktl3fe, O_RDWR) failed: No such file or directory (2) in <b>D:\foundationtests\src\assets\php\login.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>:  session_start(): Failed to read session data: files (path: \xampp\tmp) in <b>D:\foundationtests\src\assets\php\login.php</b> on line <b>2</b><br />

Now I think that the reason for this lies outside the code, so here is some info about my system: I have the most recent version of a no-install XAMPP, I run apache and mariaDB in it. This is running on a windows 10 machine, on a user account without admin privileges (thats why I chose the no-install version of XAMPP).

The website is also running inside the ZURB Foundation Framework (ZURB Template 6.4) which is based on webpack4, gulp and babel7.

EDIT: Alternatives I already tried:

suggested by "code builders" (see answers)

session_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/globallyUsedFunctions/connectToDatabase.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/globallyUsedFunctions/hashInput.php';

Result =>

<br />
<b>Warning</b>:  session_start(): open(\xampp\tmp\sess_14rr40ahtg7rbgb20fvqocet83, O_RDWR) failed: No such file or directory (2) in <b>D:\foundationtests\src\assets\php\login.php</b> on line <b>2</b><br />
<br />
<b>Warning</b>:  session_start(): Failed to read session data: files (path: \xampp\tmp) in <b>D:\foundationtests\src\assets\php\login.php</b> on line <b>2</b><br />

<b>Warning</b>: session_start(): open(\xampp\tmp\sess_4p70knkr6lb7r9ha0pitktl3fe, O_RDWR) failed: No such file or directory (2) in <b>D:\foundationtests\src\assets\php\login.php</b> on line <b>2</b><br />

The path where session_start() attempts to write its data to does not have a drive letter. Since you are running your code from D:\foundationtests\src\assets\php\login.php , it's assumed to be at D: as well. You say the actual path should be at E: . Here you are the problem.

I'm not familiar with third-party bundles (some times they seem to cause more problems than they solve) but it'll surely has a php.ini file somewhere with an incomplete session.save_path directive . Find it and fix it.

Apart from that, a better long term solution is to enable a custom session directory for each application. The mechanism is roughly the same:

  1. Create a directory in your codebase (somewhere in D:\foundationtests\src I guess) that's outside DOCUMENT_ROOT.

  2. Configure session.save_path before you call session_start() .

This has the added benefit of providing full control on session timeout.

session_start(); should always be the first line of code in your project.

Using relative paths will get you into trouble. Try using an absolute path with $_SERVER['DOCUMENT_ROOT'], then dictate where the file is.

require_once $_SERVER['DOCUMENT_ROOT']."/connectToDatabase.php";

Just create a folder "xampp/tmp" in your root directory where all your projects are. For example:

I have xampp installed (as default) on my C:/ and All Projects root directory I have is D:/ So I created a folder structure like this: D:/xampp/tmp

I had the same problem. I finally figured out it was caused by me copying the .htaccess file from cPanel. If you're using cPanel and have the same .htaccess file on your local XAMPP server, look for this line...

BEGIN cPanel-generated php ini directives, do not edit

The code below it sets up the php.ini values for the version of PHP installed on cPanel, and something in there is interfering with XAMPP. Remove that block of code (you don't need it with XAMPP) and that should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM