0

I'd like to have my website redirect to the previous page after submitting login info.

  1. I have searched around for this problem
  2. I have echoed the contents of $url and even did strcmp and it evaluates true (not shown here)
  3. Problem: The ELSE statement always evaluates even though $url == mlbmain.php OR course-website.php

Any suggestions?

<?PHP
require_once("./include/membersite_config.php");
echo "</br> </br> </br> </br>";
$url = isset($_GET['return_url']) ? $_GET['return_url'] : 'login.php';
//url now == to /mlbmain.php OR /course-website.php
$url = substr($url,1);
//url now == to mlbmain.php OR course-website.php

echo $url;  //Just to make sure
$url = trim($url);  //trim it to make sure no whitespaces
echo "</br>";
echo $url;  //Just to make sure it's still the same

if(isset($_POST['submitted']))
{       
   if($fgmembersite->Login())
   {    
        if($url == "mlbmain.php"){
            $fgmembersite->RedirectToURL("mlbmain.php");    
        }   
        else if($url == "course-website.php"){
            $fgmembersite->RedirectToURL("course-website.php"); 
        }   
        else
            $fgmembersite->RedirectToURL("index.php");  
   }

}

?>
SeaRoth
  • 177
  • 5
  • 17
  • 1
    Well, what does `$url` contain? – Pekka May 12 '13 at 19:43
  • Depends which site I am redirected from: mlbmain.php or course-website.php. I have used echo to confirm. – SeaRoth May 12 '13 at 19:46
  • are you sure even else statement evaluates and you're just not going to index.php by default – eis May 12 '13 at 19:46
  • It seems to be going to index.php by default but I would like it to go somewhere else! I can't get any if statement to evaluate true inside there it's weird! – SeaRoth May 12 '13 at 19:49
  • I don't think you can do string compares like that in php, it is probably evaluating to a pointer comparision. I have heard that == isn't safe for string compares in PHP, see http://stackoverflow.com/questions/3333353/php-string-comparison-vs-strcmp, use strcmp or === and see what happens. – Motomotes May 12 '13 at 19:51
  • The question I referenced has an answer that says `==` converts strings to floats (11 up votes) and the php man page for `==` says don't compare floats because of the way they are internally implemented, http://php.net/manual/en/language.operators.comparison.php. – Motomotes May 12 '13 at 19:55
  • @Motes you didn't apparently read that answer very well, the strings are only converted *if they appear numerics*, which is not the case here – eis May 12 '13 at 19:57
  • When I used if(strcmp($url,"/mlbmain.php")) and else if(strcmp($url,"/course-website.php")) it always evaluates the first one. When I add == 0 to both cases (str returns 0 when true) it returns me to index.html (the default location). – SeaRoth May 12 '13 at 19:59
  • You mean it always goes to `index.php`? – Amir May 12 '13 at 20:03
  • Yes, it always goes to index.php – SeaRoth May 12 '13 at 20:07
  • put `var_dump($url); die();` inside the second if block and tell us what it says – Nenad May 12 '13 at 20:15
  • I've never tried var dump!! var_dump($url) returns: string(9) "login.php" That's very interesting, and probably part of my problem.... – SeaRoth May 12 '13 at 20:27
  • @Nenad When I put var_dump($url); before the two if statements I get the correct information: mlbmain.php but when I put it inside the two if statements it returns string(9) "login.php" or nothing if I edit the $_GET statement. – SeaRoth May 12 '13 at 20:35
  • make a var_dump of `var_dump($_GET['return_url']);` inside the second if block and you will see to what the `return_url` is set – Nenad May 12 '13 at 20:37
  • @Nenad it returns NULL. How is that possible. Why are my variables getting reset and not working within this second if statement?? – SeaRoth May 12 '13 at 20:43
  • In order to help you we need to know the rest of the application. This is only a small part of it. And it also seems that you are forgetting to put `?return_url=mlbmain.php` to the URL – Nenad May 12 '13 at 20:49
  • Well could you check it out? www.crr.net23.net/mlbmain.php username: admin password: admin. – SeaRoth May 12 '13 at 21:09
  • 1
    After you press the Submit button you are making a POST request and the `return_url` variable will not be available anymore which was set with a GET request. You could create an hidden input field that will store the redirect_url and submit it with the form. – Nenad May 12 '13 at 21:43
  • 1
    @Nenad you did it! Can you copy/paste your solution as an answer so I can 'thumbs up' it ?? – SeaRoth May 12 '13 at 23:51

3 Answers3

0

Since you say

It seems to be going to index.php by default

The problem is probably either with

if(isset($_POST['submitted']))

or

if($fgmembersite->Login())

and not related to $url at all.

eis
  • 51,991
  • 13
  • 150
  • 199
  • The user is redirected to index.php, that means both of those if statements are true and working, right? They seem to be evaluating correctly because the user logs in and is redirected. – SeaRoth May 12 '13 at 20:06
  • The problem was that I needed to use a hidden form to $_POST the values to the next web page! Yahoo! – SeaRoth May 13 '13 at 00:17
0

I guess it can not find mlbmain.php or course-website.php at the current folder, so it throws 404 not found an probably you managed this error to redirect to index.php

Amir
  • 4,089
  • 4
  • 16
  • 28
0

After you press the Submit button you are making a POST request and the return_url variable will not be available anymore which was set with a GET request. You could create a hidden input field that will store the redirect_url and submit it with the form.

Nenad
  • 3,438
  • 3
  • 28
  • 36