Darkmatter5 Posted January 21, 2009 Share Posted January 21, 2009 How can I use "header('location: '');" to redirect a page to the current page. I thought "header('Location: $_SERVER[php_SELF]');" would accomplish this, but it's not working. Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/ Share on other sites More sharing options...
waterssaz Posted January 21, 2009 Share Posted January 21, 2009 what are you actually trying to achieve with this redirect? Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742316 Share on other sites More sharing options...
Mad Mick Posted January 21, 2009 Share Posted January 21, 2009 Its never a good idea to rely on PHP_SELF anyway even if you do get it to work. Why not save the address as a session variable or GET/POST variable then extract it to use for the header() function. Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742325 Share on other sites More sharing options...
Darkmatter5 Posted January 21, 2009 Author Share Posted January 21, 2009 It's a login script, after the user logs in I want the login script to redirect the user back to the page the login request came from. Say you're at page x.php and click the login button that forwards username and password to the login.php page. After the user is logged in and the session is created I want the user directed back to the page the request was made from. So the user should end up back at x.php, but logged in. The login.php file is just a "middleman" page that sets some database record changes and creates the session. Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742329 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 header("Location: {$_SERVER['PHP_SELF']}"); You were using single quotes, $ in single quotes is taken literally. Try the above. Its never a good idea to rely on PHP_SELF anyway even if you do get it to work. Why not save the address as a session variable or GET/POST variable then extract it to use for the header() function. How is using php_self bad? It cannot be modified outside since it is a server variable. I fail to see your reasoning, please elaborate for us all, thanks! Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742331 Share on other sites More sharing options...
Darkmatter5 Posted January 21, 2009 Author Share Posted January 21, 2009 Mad Mick, If this is my code: <form action='login.php?try=true' method='post'> <lable for='username'>Username: </lable><input name='username' type='text' class='text_boxes' maxlength='20'/><br> <label for='password'>Password: </label><input name='password' type='password' class='text_boxes' maxlength='20'/><br> <input name='login' type='image' src='images/login.gif' /><br> <span class='medium_text'><a href='register.php'>Register</a> | <a href='passrec.php'>Forget your password?</a></span> </form> How can I get the current page into a variable to pass to the login.php page? Maybe make a hidden form item and call it in the login.php page like the username and password items? Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742333 Share on other sites More sharing options...
Mad Mick Posted January 21, 2009 Share Posted January 21, 2009 Ah sorry - I'm thinking of PHP_REFERRER. I'll get my coat... Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742344 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 Mad Mick, If this is my code: <form action='login.php?try=true' method='post'> <lable for='username'>Username: </lable><input name='username' type='text' class='text_boxes' maxlength='20'/><br> <label for='password'>Password: </label><input name='password' type='password' class='text_boxes' maxlength='20'/><br> <input name='login' type='image' src='images/login.gif' /><br> <span class='medium_text'><a href='register.php'>Register</a> | <a href='passrec.php'>Forget your password?</a></span> </form> How can I get the current page into a variable to pass to the login.php page? Maybe make a hidden form item and call it in the login.php page like the username and password items? header("Location: {$_SERVER['PHP_SELF']}"); Try that and see if that works for ya. Ah sorry - I'm thinking of PHP_REFERRER. I'll get my coat... Np, I just wanted to make sure I did not miss something =) Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742349 Share on other sites More sharing options...
Darkmatter5 Posted January 21, 2009 Author Share Posted January 21, 2009 The problem is the header code is in my login.php or logout.php pages. If I use $_SERVER['PHP_SELF'] there, it'll be login.php or logout.php not the previous page. I tried making a hidden input in the form the login button is in that has a value us $_SERVER['PHP_SELF'] and it passes the variable to the login.php page, but won't load that page. Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742462 Share on other sites More sharing options...
Mad Mick Posted January 21, 2009 Share Posted January 21, 2009 For starters does it really need to be so flexible? Can you not just hard code the page you want to go back to in the header() function? If not then the method you are using is fine. If the page does not load then echo what the header() function is supposed to be acting upon so you can see where it thinks it should redirect. I'm sure it can only be typos etc... Link to comment https://forums.phpfreaks.com/topic/141787-redirect-to-current-page/#findComment-742665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.