- How to Remove index.php From URL in CodeIgniter 4
- Prerequisites
- How to Check index.php in URL
- Remove index.php in CodeIgniter 4
- Conclusion
- Retrieving the chosen index value from a <select> tag in PHP
- Get the selected index value of <select> tag in php
- Get the index of a certain value in an array in PHP
- Display array values in PHP
- How to change the default index for all websites on Plesk?
- Answer
- Click here to reveal additional information
- Click here to reveal additional information
How to Remove index.php From URL in CodeIgniter 4
Usually, in the CodeIgniter 4 project, you will see there is an additional argument in the URL. That is index.php in your project URL. So, if you don’t want this extra parameter then you can remove it. This will make a clean URL of your website. There are several ways to remove this index.php in CodeIgniter 4. You can create a .htaccess file and create the rule to override the URL. It will require the host to access the created .htaccess file. So, it depends upon the host like Apache or Nginx. In my last tutorial, I have shared the post on CodeIgniter 4 Multiple Image Upload. If you followed that then you have the idea of redirection of the URL with index.php after image upload. In this post, I am not going to create a new project in CodeIgniter 4 for this. I will be using an existing project here.
Prerequisites
- To remove the index.php in CodeIgniter 4, you must have an installed project. You can create a new one if you don’t have.
I am not going to show you to create a project in CodeIgniter 4 here.
How to Check index.php in URL
Firstly, I have opened the project inside the VS Code editor. I already started my development server by hitting the spark command.
Now, you can see here, the development server is started on http//localhost:8080 .
Now, I am diverting you to the browser by following the above URL.
So, I already have the image upload functionality in my last post. Now, I have navigated to the specified route and I have the below screen.
Firstly, let me show you the impact of index.php before removing from the URL in CodeIgniter 4. Here, I have already the validation rule for validating image before upload. So, I chosen the valid image and clicked on the Upload button.
Here, the image is uploaded successfully. Now, you can see the URL has been redirected with index.php by default. The below URL is http://localhost:8080/index.php/upload .
Here, this is the impact of having the index.php in the URL. So, let’s remove it in the simple steps.
Remove index.php in CodeIgniter 4
Now, come back to the CodeIgniter 4 project. Firstly, go to the app/Config/App.php. You will see the baseURL of the project.
If you want to change the base url of the CodeIgniter 4 project then you can change it from here.
In the next line, you will see the Index File description. So, you already noticed one thing while accessing the homepage of the CodeIgniter. In the indexPage property by default the index.php was added.
Here, I haven’t changed the base URL. I will access it on the default port by running the applicaton through the spark command. So, I just removed the index.php from the indexPage.
CodeIgniter 4 project has by default .htaccess file in the public folder. So, no need to create another .htaccess file.
Now, go back to the homepage and refresh the URL. Here, I am trying to upload the image again. In the result, you can see the image is uploaded and the index.php is not showing in the URL.
Conclusion
Finally, we removed the index.php from the URL of CodeIgniter 4 application. Now, the URL became neat and clean. So, you can create routes and everything will work perfectly. I hope this tutorial will help you.
Retrieving the chosen index value from a <select> tag in PHP
According to your statement, it appears that the method type (POST or GET) was not specified, therefore the default GET method was used. This is my initial experience working with PHP, and I am currently attempting to retrieve and display an array’s values.
Get the selected index value of <select> tag in php
In PHP, I encountered errors when attempting to retrieve the chosen value from the tag .
These is what I have done,
Notice: Undefined index: gender in C:\xampp\htdocs\omnama\signup.php on line 7
$Gender = isset($_POST["gender"]); ' it returns a empty string ? why ?
Could you assist me with this? I need to retrieve the chosen index value within PHP.
After going through the provided source material, I have gained knowledge on implementing the tag in PHP.
Your form is deemed acceptable. However, upon examining your complete HTML code, I noticed that you are transmitting an unassigned «default» value instead of making a selection. To remedy this, you may either follow the recommendation offered by @Vina in the comments and choose a selected option or assign a default value.
Ensure that your $_POST variables are set by checking for their existence. You have the option to assign a default value or leave them blank if they are not present.
It is crucial to prevent SQL injections.
//. $fname = isset($_POST["fname"]) ? mysql_real_escape_string($_POST['fname']) : ''; $lname = isset($_POST['lname']) ? mysql_real_escape_string($_POST['lname']) : ''; $email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : ''; you might also want to validate e-mail: if($mail = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) < $email = mysql_real_escape_string($_POST['email']); >else < //die ('invalid email address'); // or whatever, a default value? $email = ''; >$paswod = isset($_POST["paswod"]) ? mysql_real_escape_string($_POST['paswod']) : ''; $gender = isset($_POST['gender']) ? mysql_real_escape_string($_POST['gender']) : ''; $query = mysql_query("SELECT Email FROM users WHERE Email = '".$email."')"; if(mysql_num_rows($query)> 0) < echo 'userid is already there'; >else < $sql = "INSERT INTO users (FirstName, LastName, Email, Password, Gender) VALUES ('".$fname."','".$lname."','".$email."','".paswod."','".$gender."')"; $res = mysql_query($sql) or die('Error:'.mysql_error()); echo 'created';
$Gender = isset($_POST["gender"]); ' it returns a empty string
As you haven't specified the method type - POST or GET - it will default to GET method. However, you are attempting to retrieve the value using POST method, but haven't mentioned the POST method in the form. This mismatch in method will result in an empty response.
I'm confident that you will now receive the worth.
$gender = $_POST['gender']; echo $gender;
it will echoes the selected value.
Get the selected index value of tag in php, I have to get the selected index value in the PHP. I have read this link to use
Get the index of a certain value in an array in PHP
$list = array('string1', 'string2', 'string3');
My objective is to obtain the index of a specified value. For instance, for string2 , I would like to get the index of 1 . Similarly, for string3 , I want to obtain the index of 2 .
My sole requirement is to know the index of the strings within the array.
array_search is the way to do it.
array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : mixed
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1;
Instead of manually iterating over the array to find the index, you can use a function that always returns the key. This function is suitable for both associative and regular arrays.
If the array size is large or you're only performing a few of them, then utilizing array_search is a suitable approach.
$list = array('string1', 'string2', 'string3'); $k = array_search('string2', $list); //$k = 1;
If you require a considerable quantity, using a loop may be more effective.
foreach ($list as $key => $value) < echo $value . " in " . $key . ", "; >// Prints "string1 in 0, string2 in 1, string3 in 2, "
array_search() has been proposed by some individuals as it provides the index of the array element containing the value. To ensure that the array keys are sequential integers, you can implement array_values() .
$list = array(0=>'string1', 'foo'=>'string2', 42=>'string3'); $index = array_search('string2', array_values($list)); print "$index\n"; // result: 1
In your question, you mentioned that array_search() didn't serve any purpose. May I ask for an explanation? Could you clarify what you attempted and in what ways it didn't fulfill your requirements?
An issue with the array is the absence of a numerical index. You can create a zero-indexed array by utilizing array_values(). This will allow you to perform a search using array_search() without requiring a for loop.
$list = ['string1', 'string2', 'string3']; $index = array_search('string2',array_values($list));
Get the index of a certain value in an array in PHP, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams
Display array values in PHP
As a PHP beginner, I am attempting to access and exhibit an array's values. Despite extensive research, the only techniques I can come across are print_r , var_dump , or var_export . Regrettably, all these approaches produce a result that appears like this.
[a] => apple [b] => banana [c] => orange
I'm struggling with formatting the output and need to remove both [a] => and add commas. Although it's a simple task, I haven't found any helpful resources that explain how to accomplish it.
In PHP, to iterate through an array, you need to use a foreach loop.
foreach($array as $key => $value)
If your intention is to insert commas among values, contemplate using implode.
$string=implode(",",$array); echo $string;
Utilizing implode will enable you to retrieve your array separated by a string.
$withComma = implode(",", $array); echo $withComma; // Will display apple,banana,orange
The output is an array containing three elements: "apple" assigned to the key "a", "banana" assigned to the key "b", and "orange" assigned to the key "c".
Apologies for my previous message, I mistakenly misinterpreted it due to my hasty reading.
The outcome includes three fruits: apple, banana, and orange.
I have crafted a brief code excerpt that I trust will prove helpful to you.
$ages = array("Kerem"=>"35","Ahmet"=>"65","Talip"=>"62","Kamil"=>"60"); reset($ages); for ($i=0; $i < count($ages); $i++)< echo "Key : " . key($ages) . " Value : " . current($ages) . "
"; next($ages); > reset($ages);
Display array values in PHP, So, I'm working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can …
How to change the default index for all websites on Plesk?
How to change or reorder default index files (DirectoryIndex) for all websites on the server?
Answer
Click here to reveal additional information
Note: In the following example, index.php is set as a priority. All newly created domains will have the new directoryIndex afterwards.
- Connect to the server via SSH;
- Add the following lines to /usr/local/psa/admin/conf/panel.ini using any text editor: [webserver]directoryIndex = "index.php index.html index.cgi index.pl index.xhtml index.htm index.shtml"
- Reconfigure all existing domains: # plesk bin domain -l | while read i; do plesk repair web $i -y; done
For Windows
Click here to reveal additional information
Note: In the following example test.php file is set as a priority:
- Connect to the server via RDP.
- Open PowerShell command line
- Run one of the following commands depending on the requirements:
- Set up the "test.php" file as a default document file for all existing domains: Note: change the "test.php" file in the command below to the correct one. PS > plesk bin site -l | % < if ( $_ ) < &$env:windirsystem32inetsrvappcmd.exe set config $_ /section:defaultDocument /enabled:true /+files.[value=`'test.php`'] > >
- If the following error appears: ERROR ( message:New add object missing required attributes. Cannot add duplicate collection entry of type 'add' with unique key attribute 'value' set to 'main.php'. ) then such file is already present in the default documents list. Modify file priority manually in IIS > hostname > Sites > example.com > Default Documents.
- Reset the customization of Default Documents to default for all domains: PS > plesk bin site -l | % < if ( $_ ) < &$env:windirsystem32inetsrvappcmd.exe clear config $_ /section:defaultDocument >>