Im not understanding why I'm getting my 404.php file

Im doing a newsletter system, where a user enter his mail inside my input text and click on submit to subscribe in newsletter.

After he click on submit, he will receive an email, saying to confirm his subscription he needs to click in a link that I make available in this email.

This is the link:

<p>To confirm your subscription click on link below:</p>
    <a href="http://localhost/website/newsletter/confirm?email='.$email.'&amp;code='.$code.'">Confirm Subscription</a>

And this link will redirect to my newsletter confirmation page where I Will get email and code and then I will do an update on my subscribers table.

$email = $_GET['email'];
$code= $_GET['code'];
$pdo = start();
$updSub = $pdo->prepare("UPDATE subscribers set status= ? WHERE code = ?");
$updSub->bindParam(2,$code);
$updSub->execute();

But when I click in my confirmation link “Confirm Subscription”, Im acessing this URL: http://localhost/website/template/newsletter/confirma?email=emailtest@email.com&code=f14f3460bb54b54bcf33439be9c and Im entering in my “template/404.php”, in my page not found file.

Do you see why this can be happening? Im using a .htaccess file, dont know if it may be because of that, some problem with passing variables code and email in url.

This is my htaccess file:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

My query string:

@$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);

if(file_exists('template/'.$url[0].'.php')){
    require_once('template/'.$url[0].'.php');
}elseif(@file_exists('template/'.$url[0].'/'.$url[1].'.php')){
    require_once('template/'.$url[0].'/'.$url[1].'.php');
}
else{
    require_once('template/404.php');
}

Your confirm URL is 3 segments long – “website”, “newsletter”, and “confirm” – but your index if statement only allows for up to 2 segments.

Thanks for your answer Jeff Mott. But I already tried to change that, adding:

“elseif(@file_exists(‘template/’.$url[0].‘/’.$url[1].‘/’.$url[2].‘.php’)){
require_once(‘template/’.$url[0].‘/’.$url[1].‘/’.$url[2].‘.php’);
}”

but also didnt work!!

http://localhost/website/template/newsletter/confirm[COLOR=“#FF0000”]a[/COLOR]?email=emailtest@email.com&code=f14f3460bb54b54bcf33439be9c

Just a guess, but I’m thinking that your templating system is trying to find a directory or file named “confirma” rather than “confirm”

Actually, after I just saw your response to Jeff above, I’m pretty sure that’s the problem. Remove that ‘a’ from the url and see if it works then

Also, just out of curiosity, are you able to echo $_GET[‘email’] or $_GET[‘code’], or do you get a warning that those are undefined? I suspect that


RewriteRule ^(.*)$ index.php?url=$1

in the htaccess is going to cause problems in this script, that’s why I ask

Sharp eye! :slight_smile:

I’ve spent so much time fixing my own screwups, it’s second nature :smiley:

Thanks for your answers. But unfortunately, the problem is not that. I just forget to change “confirma” to “confirm” in this question because I was doing translation to english and I forgot to put in url “confirm”.
In my project I have this correctly…

So the problem remains! Do you have other ideas why this can be happening??

Im printing my $url variables in my function and when I acess my received email URL: http://localhost/website/template/newsletter/confirm?email=emailtest@email.com&code=f14f3460bb54b54bcf33439be9c

I get this messages:

Array ( [0] => template [1] => newsletter [2] => confirm )

template-> $url[0]

newsletter-> $url[1]

confirm-> $url[2]

(And it seems fine for me, I dont know why Im including my 404 really)

function getHome(){
    @$url = $_GET['url'];
    $url = explode('/', $url);
    $url[0] = ($url[0] == NULL ? 'index' : $url[0]);
    print_r($url);
    echo '<br/><br/>'.$url[0].'-> $url[0]<br/><br/>';
    echo $url[1].'-> $url[1]<br/><br/>';
    echo $url[2].'-> $url[2]<br/><br/>';
    if(file_exists('template/'.$url[0].'.php')){
        require_once('template/'.$url[0].'.php');
    }elseif(@file_exists('template/'.$url[0].'/'.$url[1].'/'.$url[2].'.php')){
        require_once('template/'.$url[0].'/'.$url[1].'/'.$url[2].'.php');
    }
    elseif(@file_exists('template/'.$url[0].'/'.$url[1].'.php')){
        require_once('template/'.$url[0].'/'.$url[1].'.php');
    }
    else{
        require_once('template/404.php');
    }
}

If $url[0] is “template”, and $url[1] is “newsletter”, and $url[2] is “confirm”, then this if statement is checking if “template/template/newsletter/confirm.php” exists. I’m assuming the double “template” is unintentional?

Thanks Jeff, the problem seems that was what you said. I dont understand how I didnt see this! Thank you really!