chaseman Posted March 30, 2011 Share Posted March 30, 2011 I have no clue about JavaScript, I'm just now learning PHP and building a website. I wanted to ask if somebody can recommend me a script to redirect the user to the main page after a successful login with a 5 second delay? I've tried searching the web, but surprisingly I couldn't find anything, you'd think this has been done many times by many people. I hope to get some suggestions. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/ Share on other sites More sharing options...
brianlange Posted March 30, 2011 Share Posted March 30, 2011 use the setTimeout javascript function http://www.w3schools.com/js/js_timing.asp Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194405 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 Do you need to redirect the user like that? Like the META refresh tag they can get users "stuck" temporarily when trying to go back in the browser's history. It's bad for usability and quite a dated concept these days - I mean do you really need to be told you have logged in after you just filled out a login form? Why not just take them straight back to the page they were at before? That's what the user really wants; speed and ease! Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194406 Share on other sites More sharing options...
chaseman Posted March 30, 2011 Author Share Posted March 30, 2011 use the setTimeout javascript function http://www.w3schools.com/js/js_timing.asp I've found those type of techniques on the web, though, most tutorials showcase how to insert an onLoad into the body, my body is in the header.php file together with the navigation bar, which would mean that every page gets redirected. What would be a way I could implement that into that specific page, for example, into a DIV instead of into the body tag? Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194438 Share on other sites More sharing options...
chaseman Posted March 30, 2011 Author Share Posted March 30, 2011 This is how I solved the problem: <script type="text/javascript"> function delayedRedirect(){ window.location = "01.php" } </script> </head> <body <?php $url = $_SERVER['PHP_SELF']; $path = pathinfo($url); $filename = $path['filename'] . "." . $path['extension']; if ($filename == 'successful_login.php') { ?> onLoad="setTimeout('delayedRedirect()', 3000)" <?php } ?> > I guess there's a creative programmer in me haha, I like this solution a lot. This way I don't have to use two different header files and I get the redirect I wanted. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194534 Share on other sites More sharing options...
Adam Posted March 31, 2011 Share Posted March 31, 2011 $url = $_SERVER['PHP_SELF']; $path = pathinfo($url); $filename = $path['filename'] . "." . $path['extension']; // Is the same as.. $filename = basename($_SERVER['PHP_SELF']); Issue I'd raise with this, is that if you change the file name or want to re-use the code you have a very limited implementation. Personally I'd check for a flag within the header (which is the filename to redirect to), but define that within the code that includes the header. For example (and only guessing certain parts of your code obviously)... successful_login.php: [...] // Set the redirect flag $javascript_redirect = '01.php'; // Include the header require_once 'header.php'; [...] header.php: [...] <?php if (!empty($javascript_redirect)) { ?> <script type="text/javascript"> function delayedRedirect(){ window.location = '<?php echo $javascript_redirect; ?>'; } </script> <?php } ?> [...] This also has the benefit of splitting the application - "business" - logic from the template - "view" - logic, whilst making it easily re-usable. As I think I made fairly clear I'm not a fan of this kind of thing, but if you want to do it may as well do it well Edit Forgot to mention, the onload event would also need an !empty() check on the flag, but I'd recommend assigning that within the same block of JavaScript: window.onload = function() { // ... } Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194843 Share on other sites More sharing options...
chaseman Posted March 31, 2011 Author Share Posted March 31, 2011 Thanks for improving the script! Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1194884 Share on other sites More sharing options...
jamesjmann Posted April 1, 2011 Share Posted April 1, 2011 This is how I solved the problem: <script type="text/javascript"> function delayedRedirect(){ window.location = "01.php" } </script> </head> <body <?php $url = $_SERVER['PHP_SELF']; $path = pathinfo($url); $filename = $path['filename'] . "." . $path['extension']; if ($filename == 'successful_login.php') { ?> onLoad="setTimeout('delayedRedirect()', 3000)" <?php } ?> > I guess there's a creative programmer in me haha, I like this solution a lot. This way I don't have to use two different header files and I get the redirect I wanted. Why not just use ajax? You could use ajax to log the person in, and THEN redirect after a certain period of time. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195532 Share on other sites More sharing options...
Adam Posted April 1, 2011 Share Posted April 1, 2011 Why use AJAX if you're just going to redirect anyway? Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195539 Share on other sites More sharing options...
jamesjmann Posted April 1, 2011 Share Posted April 1, 2011 Why use AJAX if you're just going to redirect anyway? You're going to want to use a redirect in any case, but I'd rather use ajax, than just plain old PHP. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195562 Share on other sites More sharing options...
Adam Posted April 1, 2011 Share Posted April 1, 2011 Why? So that it takes twice as long and adds a dependency on JavaScript? Not trying to undermine you or anything but there's no logic in that at all. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195571 Share on other sites More sharing options...
jamesjmann Posted April 1, 2011 Share Posted April 1, 2011 Why? So that it takes twice as long and adds a dependency on JavaScript? Not trying to undermine you or anything but there's no logic in that at all. It takes one millisecond if that for my scripts to make ajax calls. And i consider it a huge importance having plain php as a backup plan case the user doesnt have javascript. Im just saying that one should always try to use javascript/ajax first, as it improves and enhances the gui and usability of the website, which im sure everybody wants. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195733 Share on other sites More sharing options...
chaseman Posted April 1, 2011 Author Share Posted April 1, 2011 This is how I solved the problem: <script type="text/javascript"> function delayedRedirect(){ window.location = "01.php" } </script> </head> <body <?php $url = $_SERVER['PHP_SELF']; $path = pathinfo($url); $filename = $path['filename'] . "." . $path['extension']; if ($filename == 'successful_login.php') { ?> onLoad="setTimeout('delayedRedirect()', 3000)" <?php } ?> > I guess there's a creative programmer in me haha, I like this solution a lot. This way I don't have to use two different header files and I get the redirect I wanted. Why not just use ajax? You could use ajax to log the person in, and THEN redirect after a certain period of time. Are you talking about those dynamic login menus (like on Twitter) where you click on the button and a small box slides down and then you get your login form? The simple reason why I didn't do that is because I don't know how to do it, I started learning PHP 3 month ago. If you have a tutorial to link me to I'd have a look it. Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195736 Share on other sites More sharing options...
jamesjmann Posted April 2, 2011 Share Posted April 2, 2011 Ive only been learningphp for 3 months too and i already know ajax like my native language! Haha. No but really ajax isnt that hard. And you would use jquery to slide the login form out, ajax would be for handling the php script without having to reload/refresh the page. Both are, in essence, just javascript, so id start by going to borders or amazon.com and look for a book on javascript, ajax, jquery, or a combination of two or more of the above. I learned javascript in less than a week; its easy to learn because the syntax is darn near identical to that of php (save for declaring variables and all the various functions...). Quote Link to comment https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/#findComment-1195871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.