Not able to create new directory with variable (PHP) - php

Hello I'm trying to create a directory in the users/ folder. When the user registers a vpb_save_details.php is being opened. And mkdir is ran to create a user with the username as directory name.
The user is being registered as normal but the problem that no directory is created, not even the /user directory.
If i run this code:
<?php
mkdir("blah", 0777);
?>
The directory bla is created, but if I run my code vpb_save_details.php, nothing happens.
This is a part of the code:
//If the user is validated and every thing is alright, then save the user details and display success message to the user
//otherwise, display error message to the user
if ($vpb_error == '')
{
//Encrypt user information with Base64 before saving them to a file
if(fwrite($vpb_database, "\r\n".base64_encode($fullname)."::::::::::".base64_encode($username)."::::::::::".base64_encode($email)."::::::::::".base64_encode($encrypted_user_password).""))
{
echo '<font style="font-size:0px;">completed</font>';
echo '<div class="info">Congrats <b>'.$fullname.'</b>, you have registered successful. <br>You may now click on the login button to log into your account.<br>Thanks.</div>';
//
// HERE! I create a folder in the users directory with the variable $username as username
//
$newUserName = $username;
$userPath = "users/".$newUserName;
mkdir("$userPath", 0777);
//copy("index.php",$userPath/index.php);
}
else
{
echo '<div class="info">Sorry, your account creation was unsuccessful, please try again (1).</div>';
}
}
else
{
echo $vpb_error;
}
fclose($vpb_database);
I have absolutely no idea what the problem could be? the code:
$newUserName = $username;
$userPath = "users/".$newUserName;
mkdir("$userPath", 0777);
Should work just fine, or am I missing a small detail?

Remove the quotes.
mkdir($userPath, 0777);

Related

Redirect page after delete account in php

I need a little help here. I have a page profile.php and a option to delete the accound :
// DELETE THE ACCOUNT !!
$_SESSION["delacc"] = FALSE;
if (isset ($_POST ['deleteaccount'])) {
$deleteaccount = $_POST['deleteaccount'];
$delacc="DELETE FROM users WHERE username='$username'";
$resdelacc = mysqli_query($con,$delacc);
if ($resdelacc) {
header('Location: index.php');
$_SESSION["delacc"] = TRUE;
unset($_SESSION['username']);
} else {
echo "ERROR !!! Something were wrong !!";
}
}
the problem is in if ($resdelacc). If this is true, result that the account was deleted, unset session username (logout) and after this I want to redirect the page to index.php where I have the code :
if(isset($_SESSION["delacc"])) {
if($_SESSION["delacc"] == TRUE) {
echo "<b><font color='red'>YOUR ACCOUNT WAS SUCCESFULLY DELETED !!</font></b>";
$_SESSION['delacc'] = FALSE;
}
}
My only problem is that this line " header('Location: index.php');" (from profile.php) don't run in any case. When the user click the button "DELETE ACCOUNT", the page remain profil.php, then, if do refresh or access another page, is redirected and appear as guest.
Very easy .. The reason is after in the resulted output page you can't redirect. so you've prepare it to be redirected after some seconds enough for user to read the result message.
Like this:
if($_SESSION["delacc"] == TRUE) {
$_SESSION['delacc'] = FALSE;
echo '<!DOCTYPE html><html><head><meta http-equiv="refresh" content="7;url=http://'.$_SERVER['HTTP_HOST'].'/index.html"/>';
echo "</head><body>";
echo "<b><font color='red'>YOUR ACCOUNT WAS SUCCESFULLY DELETED !!</font></b>";
}
that change will redirect to the index.html after 7 seconds.
PS. The Generated HTML result page make it starts by this code after the POST handling direct. (before any echo) because echo will start generating the results page and the only logical place to redirect is inside the HEADER before any BODY elements
<meta http-equiv="refresh" content="0";url="/index.php"/>
The redirect (url) don't run for index.php because I have another redirect before :
if(isset($_SESSION['username'])==FALSE) {
header('Location: login.php');
}
but is ok, I put the message "DELETED SUCCESFULLY" in login.php and deleted from index.php . I set content=0, because after deleted, the user will be restricted for page profile.php and need to change immediatelly to another. Due of the verification of SESSION['username'] which can return profile.php, I can not redirect to another page ... is a conflict. I need a little to think better this code with redirects, I know can solve it better :D thanks for explanations and help

Sessions in php do not load profiles, php + mysql

I need to be able to orientate with a php code that communicates with data from a mysql database, this file is called "validate.php". Its main functions are to verify that there are no empty fields at the time of login, and assign a profile if a user has value 1 and another profile when the value is 0 in the records of the table "users"
The idea is that "validate.php" check the user and direct it to a page according to their profile, but I can not do that.
My code is:
<?php
require('access_db.php');
session_start();
if(isset($_POST['send'])) { // We verify that the form data has been sent
//We verify that the user_name and the user_pass fields are not empty
if(empty($_POST['user_name']) || empty($_POST['user_pass'])) {
echo"
<script>
alert('Please enter your username and password correctly ');
location.replace('login.php');
</script>
";
}else {
//"Clean" the form fields of possible malicious code
$user_name = mysqli_real_escape_string($link,trim($_POST['user_name']));
$user_pass = mysqli_real_escape_string($link,trim($_POST['user_pass']));
// We verify that the data entered in the form match those of the DB
$query=mysqli_query($link,"select user_id,user_name,user_admin FROM users WHERE user_name='".$user_name."' and user_pass ='".$user_pass."'");
$row = mysqli_fetch_array($query);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row["user_name"];
$_SESSION['user_admin'] = $row["user_admin"];
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
{
}
}else{
header("Location: login.php");
}?>
My main problem is here:
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
When I login with my admin user in my page "login.php" you should check the information and go to a page according to your profile, only appears in the browser "http://localhost/proyect/validate.php" and the text "dashboard" on the page, But, if I write in the browser "http://localhost/proyect/dashboard.php" load the page with all the information.
I do not know what I'm doing wrong.
Someone can help me, I'll be very grateful, I've been on this for days.
Thanks.
Don't print, try this instead:
if($_SESSION['user_admin']==1){
header('location:dashboard.php');
exit;
}else{
header('location:dashboard2.php');
exit;
}
Thanks for the suggestion Magnus Eriksson
you need to redirect not echo out the contents of the php file
and also do check for { as there are extra ones
if($_SESSION['user_admin']==1){
header("Location: dashboard.php");
}else{
header("Location: dashboard2.php");
}

My PHP code not creating a directory

<?php
error_reporting(0);
$title='New Account'; /*This is a page that creates new account*/
include('header.php'); /*header file included*/
extract($_POST);
if(isset($r9)) /*$r9 is submit */
{
if(!empty($r1) && !empty($r2) && !empty($r3) && !empty($r4) && !empty($r5) && !empty($r6) && !empty($r7) && !empty($r8) )
{
if(is_dir("user/$r3")) //$r3 is the email the person is providing
{
echo "User already registered";
}
else
{
mkdir("user/$r3"); /*directory creation*/
file_put_contents("user/$r3/details.txt","$r1\n$r2\n$r4\n$r5\n$r6\n$r7\n$r8");
header("location:welcome.php"); /*the next page it must go after this*/
}
}
}
?>
This is my PHP code and it is not creating the directory. Please help me out.
Please turn on the error reporting to see the error messages, that will help you to solve this problem. Please check if your web server has permissions to creating a new directory. If your web server has the permissions already to write in the folder user then try to edit the following code:
mkdir("user/$r3");
to
mkdir("/THE_HOLE_URL_TO_THIS_FOLDER/user/$r3");
for example mkdir("/opt/lampp/htdocs/myhomespace/user/$r3");

how to redirect to a page and then force_download or vice-versa after login in CI

I am using CI and using force_download successfully. Now I want user to first login for download. So far so good and I am able to do it. The problem is when user isn't logged in on click of download it redirects to login page and then only do the download. Now I want is when user logs in for download I want,
1. to redirect to download page first and then do the force download
or
2. do force download and then redirect to download page
How am I to do this. I tried redirecting to page after force download but to no avail. So far I don't want to use session. Any other option is highly welcome. Below is my function for download.
Thanks in advance.
function index($folder, $placeholer, $file){
$this->load->helper('download');
$fullname = $this->session->userdata('full_name');
if(!empty($fullname)){
if(file_exists("./administrator/uploaded_files/".$placeholer."/".$file)){
if($placeholer == 'article_files')
$tblName = 'tbl_articles';
else
$tblName = 'tbl_discussion_papers';
$original_filename = $this->public_model->get_single_data($tblName, 'original_filename', $file);
$data = file_get_contents("./administrator/uploaded_files/".$placeholer."/".$file);
force_download($original_filename, $data, $folder);
}else{
echo "File doesn't exists.";
}
}else{
$data['login'] = 'Login first to Download';
$data['class'] = 'error';
$data['red_url'] = 'download/'.$folder.'/'.$placeholer.'/'.$file;
//$data['downlod_url'] = $folder;
$this->session->set_flashdata($data);
redirect('login');
}
}

Catch session creation error in PHP

I wanted to be able to catch the error in session creation in PHP. My primary target is to be able to know which causes the problem. Is it because we don't have wrtie access to the folder where the session is written or because we don't have free disk space already.
So of course we will start with this.
session_start();
and from this post Check if session is set or not, and if not create one? I did try to add
if(session_id() == '')
{
// session has NOT been started
session_start();
echo "Session is started";
}
else
{
// session has been started
}
And to test this I have remove writing permissions for the group and others in /tmp/ by using this command
chmod 755 tmp/
But base on my test I still see that the session is started. And the funny thing is I can login but when I try to logout I can't.
Any insights on how to properly get the reason on session creation error would be greatly appreaciated. Thanks!
EDIT:
I have tried Frederik's suggestion and done this.
try{
// Start session
session_start();
echo "Session started. " . session_id();
}catch(Exception $e){
echo "Session not started. " . $e->getMessage();
}
But I still see this
Session started. fa505cbf4b232773669008495fd08b9f
even if I have remove write permission to /tmp already. It seems that the try catch was not able to properly catch the problem.
You might want to try the is_writable function.
Something like:
<?php
$dirname = '/tmp';
if (is_writable($dirname)) {
echo 'The folder is writable';
} else {
echo 'The folder is not writable';
}
?>
You can use it with files or folders.
More information: http://id1.php.net/is_writable
With Frederik's suggestion to check if the /tmp do have write permission I was able to decide to just check for write permission and disk space before checking for username and password. So I wrote this code
if (is_writable(session_save_path())){
if ( $diskfree is <= 0){
// check for authentication here
}else{
header("Location: login.php?error=true&nodisk=true");
return;
}
}else {
header("Location: login.php?error=true&nowrite=true");
return;
}
and then in login.php I have this to catch the error code.
// If we have an error show error message
if ( isset($_GET["error"]) && $_GET["error"] ) {
//If not enough disk space show disk space message
if( isset($_GET["nodisk"]) && $_GET["nodisk"] ){
alert("disk free problem");
}
//If we don't have permission to session save path show session creation error
if( isset($_GET["nowrite"]) && $_GET["nowrite"] ){
alert("write permission error");
}

Categories