尝试使用fwrite()将包含PHP的文件写入新密码

I have a PHP script that is executed when a user submits a Change Password Form. It runs great and I have had success writing to the file when using the fgets() and fgetc() methods (this required a lot of code). But I never wanted to do it this way. I want to be able to call the $_SESSION["password"] from my pass.php file and replace it with $newpass using the fwrite() method. The current code does not replace at all.

My PHP script.

<?php
$oldpass = $_SESSION["password"] ;
$newpass = $_POST["newpass"] ;
$session =  fopen( "pass.php" , "r+" ) or die( "Could not open file" )  ;

$session ;
$content = file_get_contents( $session ) ;
$content = str_replace( $oldpass , $newpass , $content ) ;
fwrite( $session , $content ) ;
fclose( $session ) ;
?>

My external PHP file pass.php.

<?php
session_start() ;
$_SESSION["username"] = "imauser" ;
$_SESSION["password"] = "imapass" ;
?>

The error is in file_get_contents($session). file_get_contents expects a filename not a resource.

PHP manual