Is there a way to do a one time redirect? Where it goes to an entrance page, and says enter site, then you go to the index.html page.
the best example of what I'm trying to accomplish: www.matisyahuworld.com
There's a page order.html that's the first page you see before you can go to the index.html page
thanks
Yes, you would have to check if the user is new or not. The only way I know how to do that is to use javascript cookies :)
document.cookie = 'unique_user = second time visitor;
expires = date you want; path=/'
Of course everything is made easier with jQuery:
$.cookie("example", "foo");
The logic would be, check if your cookie exists within the clients browser, if it does, don't put up splash page and redirect straight to index.html. If there is no cookie send it to splash, splash.html, and set the cookie there.
Here is a great resource on how to do that:
Javascript Cookies
setcookie is more simple to use:
$expire=time()+60*60*24*30;
setcookie(user,visited, $expire);
if(isset($_COOKIE["user"]))
{
RedirectToURL("index.html");
}
To create function RedirectToURL
function RedirectToURL($url)
{
header("Location: $url");
exit;
}
NOTE : Above code can be used for PHP scripting.Yet it is simple .Just add the above lines in
<?PHP
and end with
?>
also you must save the file with .php extension
Related
Does anybody know how to redirect someone from a page like example.com/2458/233 to example2.com/2458/233?
On the first url, there is only a file who is redirecting to that other domain. Does anybody know how to do it?
A better explanation:
We have a URL example.com/222/222 and I only want an index file that would take the /222/222 and redirect to example2.com/222/222
so just set the location with the pathname
window.location.href = 'http://www.example2.com' + window.location.pathname
This will not keep querystring or hash. If you want that, you can do a simple replacement/substring...
window.location.href = window.location.href.replace(window.location.origin, 'https://example2.com')
but in the end this is not good for SEO, better with a serverside redirect.
Name this "index.php".
Edit the location of the page you are redirecting to.
Save it to the directory that you want to send it from.
<?php
/* no one should be here, send them away! */
exit(header("Location: ../"));
?>
I am trying to have page1.php redirect to page2.php after 5 seconds. However page2.php needs to be a restricted page that can only be viewed if you are being sent from --> mydomain.com/page1.php and can not be accessible if you type the address manually into the address bar.
I have tried methods that use shared keys, htaccess and php HTTP_REFERRER.
I believe the issue is coming from the redirection, and I believe it is because the redirect script is not sending the HTTP_REFERRER and therefore page2.php is looking at the url sent from the redirection script as being manually entered. I have tried with a simple php redirect and javascript. Below are the two different redirect scripts I have used.
php version.
header( "refresh:5;url=page2.php" );
Javascript version.
<script type="text/javascript">
function Redirect()
{
window.location="page2.php";
}
setTimeout('Redirect()', 5000);
</script>
I have tried these with the full url and with/without http:// for example mydomain.com/page2.php.
Page2.php needs to only accept traffic from page1.php. I have no objection as to how to go about achieving this. Using shared keys or any other aspect just as long as the user can not enter the address manually and visit the page. I am also fully aware the Referrer can be spoofed however I do not have the expertise to get to advanced.
You can use session data to make sure users of page2 have passed through page 1
With the way sessions work,The encrypted string is quite secure even if it is not encrypted at all.
on page1:
session_start();
$_SESSION['secret_key'] = 'encrypted_string';
on page2:
session_start();
if($_SESSION['secret_key'] == 'encrypted_string'){
// user is authorized
echo 'You are authorized to see this page';
}
else{
echo 'Please visit page1 before accessing this page';
}
// Logic for authorized user
Or, shorter version for page2 :
if(empty($_SESSION['secret_key']) || $_SESSION['secret_key'] != 'encrypted_string'){
die('You are not authorized to view this page.');
}
echo 'only authorized user will see from here forward';
BTW, when testing, remember that once your session is set, you will have to delete sessions in browser, or use incognito to test again.
To delete cache on chrome ctrl+shift+delete and choose cookies and other
Here's how I would do it, using 3 pages.
On the landing page, include your JavaScript, this will redirect you to an intermediate page that sets a session variable before redirecting to the final page.
On the final page, check the session variable, determine whether or not to display the page, then unset the session variable (so if they try again without returning to the first page, it will no longer work).
p1.php
<?php session_start(); ?>
<script type="text/javascript">
function Redirect()
{
window.location="p12.php";
}
setTimeout('Redirect()', 5000);
</script>
p12.php
<?php session_start();
$_SESSION['secret'] = 'todays_password';
$newURL = 'p2.php';
header('Location: '.$newURL);
?>
<script type="text/javascript">
function Redirect()
{
window.location="p2.php";
}
Redirect();
</script>
p2.php
<?php session_start();
if (isset($_SESSION['secret']))
{
if ($_SESSION['secret'] == 'todays_password')
{
//The user provided the correct secret session variable
echo 'welcome. you can view this page.';
//Put all of your page content here
?>
<!-- HTML content should be in between php delimiters, like this-->
<?php
}
else
{
//The user supplied a secret code, but it was not the correct one
echo 'invalid secret.';
//You can also add code for redirecting the user back to p1 here
//Or just display an error message
}
}
else
{
//The user did not provide a secret session variable -- they most likely did not pass through p12.
echo 'error, you are unable to view this page';
//You can also add code for redirecting the user back to p1 here
//Or just display an error message
}
unset($_SESSION['secret']); //This line makes the user return to p1 every time they visit p2 -- delete this line if you only want them to visit p1 once.
?>
To make this method secure, you'd need to give each user a unique value for their secret session variable. Store this variable, along with it's timestamp when the user visits p1 both as a session variable for the client and in a server-side database. When p2 is loaded, check to see if the session value they provide is at least 5 seconds old in the database. If it is, let them see the page. Then delete the value in the database.
I use document.referrer to see if a user has added a new message on my application. The page where the messages are shown is called 'messages.php' and the script that handles adding messages is called 'add_message.php'.
I want to create an effect on the last added message, but only if the user has just added the message. To see if the last message was just added and wasn't there before, I need to see if the user's last visited page was 'add_message.php'.
This is the code I use to detect the last visited URL with an if statement to check if that's the case:
var prevURL = document.referrer;
var newMessageURL = 'add_message.php';
if(prevURL.indexOf(newMessageURL) > -1) {
alert(prevURL);
}
The problem is that when I add a message, the script 'add_message.php' is called (via form action), however, document.referrer returns 'messages.php' as the last visited page, but it should be 'add_message.php', because that's where I come back from when I add a new message.
How can I get document.referrer to return 'add_message.php' when I add a message?
In the 'add_message.php' I use
header('Location: ' . $_SERVER['HTTP_REFERER']);
Does this cause the issue?
Referrers are unreliable at best, especially as browsers come with the option to spoof or hide it completely. In this case, because it's a redirect, the "in-between" page doesn't get counted as the last page you visited.
That said, since you're using PHP, there's another way:
As part of your add_message.php file, add this:
// assuming you already have session_start() somewhere above
$_SESSION['just_added_a_message'] = true;
Then, where you have your JavaScript now, replace it with PHP:
<?php
if( !empty($_SESSION['just_added_a_message'])) {
?>
<script>alert("Ohai there!");</script>
<?php
unset($_SESSION['just_added_a_message']);
}
?>
I use this technique myself to show a confirmation "You sent a Personal Message to X" message when the user has been redirected back to their Inbox after sending.
Whenever I user php header("Location: index.php"), most of time then it does not work. It exactly DOES NOT work without any error nor any run-time fatal error. I don't use any echoing nor I keep empty lines prior to header() call. I double check everthing, but it does not work.
Now, I user JS solution. I write a function that gets the param, store it in an immediately created <input> field and the using Javascript in the same function, I get the value out of input and assign it to window.location.href= value; which then finalizes the redirection. Is my method reliable and good?
My function seems like this:
function redirect($address)
{
?>
<input id='this_address' value="<?php echo $address; ?>" />
window.location.href = document.getElementById("this_address").value;
<?php
}
Are you sure that your index.php is in the same directory as the scripts that calls redirect or vice versa...?
If not try header("location:../../index.php");
Setting and cookies or session variables will cause issues with a redirect. It has to be sent before any output or exchange with the server and the client. Check your browser log for any errors too. It might be helpful in determining why you are not achieving the result you want.
You could also implement both methods, but it would be more useful to find out why your PHP redirect is not effective.
If u can't able to find out the solution for php header prob, Just add the below code at the top of the page. This is not the perfect way, but we can use this...
ob_start();
Why not use Response.Redirect "../../index.php" to redirect to another page.
The PHP code below echoes a link if the "loginid" of the logged-in user is on a list determined by getEditorList();. It works fairly well, but I think it might work better if I were to do it with Javascript instead.
How could I accomplish the same thing with Javascript?
Thanks in advance,
John
$editors = getEditorsList();
foreach($editors as $editor)
{
$editorids[] = $editor['loginid'];
}
if(in_array($_SESSION['loginid'], $editorids))
{
echo "<div class='footervote'><a href='http://www...com/.../footervote.php'>Vote</a></div>";
}
Login function:
<?php
if (!isLoggedIn())
{
if (isset($_POST['cmdlogin']))
{
if (checkLogin($_POST['username'], $_POST['password']))
{
show_userbox();
} else
{
echo "Incorrect Login information !";
show_loginform();
}
} else
{
show_loginform();
}
} else
{
show_userbox();
}
?>
So actually, your problem is "After a user logs in, he often still gets the non-logged in version of the pages", and you propose the "Make the link appear in Javascript" as a solution. Then you ask how to do that :)
RoToRa already pointed out that the cause of your problem probably has to do with the browser cashing the pages, and not requesting a new page after the user has logged in, but showing the old page from cache. There are several solutions for this, even with Javascript.
To avoid caching:
Add a 'Pragma: No-Cache' header to your page.
Add a 'Expires: -1' header to your page.
Use the Cache-Control HTTP headers from HTTP 1.1
Check the PHP header function for more information how to do that in PHP, or read your webservers documentation.
Another possible solution is to have the page serve as static, and then use AJAX to check if the user is logged-in, and if so, load the custom content for that user. Typically you'll create an extra php-script to check if the user is logged in, and have the AJAX call this script. This is quite easy to do using a decent Javascript library like jQuery, but you can do it without libraries should you really need to do so.
Checkout PHP JSON, jQuery AJAX for more info about those solutions.
A last often (ab)used solution is to have a query string for your page - browsers will see the query string is different, and won't cache the page.