Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/232183-delayed-redirect-after-successful-login/
Share on other sites

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!

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?

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.

$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() {
    // ...
}

 

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.

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.

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.

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...).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.