CodeIgniter Forums

Full Version: Redirect issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello again!

Im tryting to redirect one page to another but having a hard time. I added the controller/function in HEADER field and hit submit but keep getting a 404 error and in url space this is what appears: http://culturedkink.com/index.html. Im trying to figure out why index.html show up? Here is the php code for the page:

Code:
<?php
// Include config file
require_once "application/config/database.php";
 
// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";
 
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
 
    // Validate username
    if(empty($_POST["username"])){
        $username_err = "Please enter a username.";
    } else{
        // Prepare a select statement
        $sql = "SELECT id FROM users WHERE username = ?";
        
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_username);
            
            // Set parameters
            $param_username = trim($_POST["username"]);
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                /* store result */
                mysqli_stmt_store_result($stmt);
                
                if(mysqli_stmt_num_rows($stmt) == 1){
                    $username_err = "This username is already taken.";
                } else{
                    $username = trim($_POST["username"]);
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
         
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Validate password
    if(empty($_POST["password"])){
        $password_err = "Please enter a password.";     
    } elseif(strlen(trim($_POST["password"])) < 6){
        $password_err = "Password must have atleast 6 characters.";
    } else{
        $password = trim($_POST["password"]);
    }
    
    // Validate confirm password
    if(empty($_POST["confirm_password"])){
        $confirm_password_err = "Please confirm password.";     
    } else{
        $confirm_password = trim($_POST["confirm_password"]);
        if(empty($password_err) && ($password != $confirm_password)){
            $confirm_password_err = "Password did not match.";
        }
    }
    
    // Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
        
        // Prepare an insert statement
        $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
         
        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
            
            // Set parameters
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
            
            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                
               header("location:".base_url()."index.php/register/log");
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }
         
        // Close statement
        mysqli_stmt_close($stmt);
    }
    
    // Close connection
    mysqli_close($link);
}
?>

** EDITED TO USE BBCODE SO IT DOESN'T RUN ON**

Im trying to figure why html comes up and not php. Do I create another controller or make changes to another file. BTW register/log is controller and function.

Heart Heart ,
Mekaboo
@Mekaboo,

Why don't you just do a straight redirect ( https://www.codeigniter.com/user_guide/h...l#redirect ). That seems like it would be easier.
What kind of file is the code you posted here? Model, controller, view???  I think you are (still) missing the basics about CodeIgniter.
Your code only shows procedural code, where CodeIgniter is object oriented, and works with classes. Your php file doesn't contain any class.
CodeIgniter will process all URL's through index.php and never points to html files.
(05-10-2019, 06:39 AM)php_rocs Wrote: [ -> ]@Mekaboo,

Why don't you just do a straight redirect ( https://www.codeigniter.com/user_guide/h...l#redirect ).  That seems like it would be easier.

Ill try this, Thanks Heart
(05-10-2019, 10:30 AM)Wouter60 Wrote: [ -> ]What kind of file is the code you posted here? Model, controller, view???  I think you are (still) missing the basics about CodeIgniter.
Your code only shows procedural code, where CodeIgniter is object oriented, and works with classes. Your php file doesn't contain any class.
CodeIgniter will process all URL's through index.php and never points to html files.

Sorry about that! Its for a view, I copied code from github and added to mine.
PHP Code:
// chage this to below
header("location:".base_url()."index.php/register/log");

// load the url helper
redirect(base_url('index.php/register/log')); 

Try that
(05-11-2019, 08:26 AM)InsiteFX Wrote: [ -> ]
PHP Code:
// chage this to below
header("location:".base_url()."index.php/register/log");

// load the url helper
redirect(base_url('index.php/register/log')); 

Try that

Thank you so very much I got it now Heart I took the code from here:

https://www.codeigniter.com/user_guide/l...ation.html

I was missing the form validation Blush