Stopping the use of a URL to get to the site - javascript

So I have a website which redirects to another part of the site ie, when pressing a button on "www.test.com" it reditects to "www.test.com/anotherpage". Now my problem is that I only want users to be able to access the second page when using the button and not just typing in "www.test.com/anotherpage" into the search bar.
So, is there any way to setup my page such that it cannot be accessed by the url but only redirected from another part of the website?

document.referrer
The value is an empty string if the user navigated to the page directly (not through a link, but, for example, via a bookmark). Since this property returns only a string, it does not give you DOM access to the referring page.

You need use Php code to login users and check sessions example like exaplain here: Login form and session example
of course you need basic know about Php

You could use a client or a server side technique.
Example of client side is Javascript, reading the document.referrer in the called page as #Barmar pointed out, and redirecting to the home in case the referrer is not from your home page.
The server side way could make use of parameters passed to the page, like a SESSION identifier that will be generate on the home page, for example and checked in the called page.
Actually there are a different ways to accomplish your goal.
You should provide some more info about the environment so we can help.

You can usee PHP. Simply put your button between form tags
<form action="http://www.test.com/anotherpage" method="POST">
<input name="mybutton" type="submit" value="Go to another page">
</form>
and on anotherpage
<?php
if (!isset($_POST['mybutton'])) { //check if the button was clicked
header('Location: index.html'); //forward to index if not
}
?>

Related

How would custom user links work?

By custom user links, i mean like for example when a user registers to the website, a page is created specifically for that user with a link.
For Example
https:/domain.com/users/customerName
Then after creating the link, the website will automatically customize the website by using a clone of a specific webpage.
*Btw i've already took Care of the Login/Register part. I just need to know how custom user links would work.
Option 1: example.com/user
Use a single PHP file and an .htaccess file. Check out How to create friendly URL in php?
Option 2: user.example.com
Create sub-domains for each user, also uses .htaccess. Check out How to let PHP to create subdomain automatically for each user?
Option 3: example.com?user=name
Create a single php file and use $_GET parameters. This is the most usual and easiest way to customize the website based on the user who registered and logged in. (usually using user ID number: example.com/profile.php?user=71)
Of course there's also Session handling.
I think you searching for URL rewriting concept.
If user login the page no need to clone the page.you could access the same page with this user data and specification(dynamic page).Many the page content with php functions
URL rewriting
you could the function in .htaccess
if user enters the page
http://example.com/someuser
its rewrite the url with
http://example.com?q=someuser
if you see the url bar its like special page for the user.
It's actually fairly simple. You just use GET within PHP and the URL would be something like http://example.com/user?id=4453623 - If you've ever been on facebook you'll notice they use PHP for the profile pages and much other things too. If you go to your profile page, you'll notice a "id=" variable up in the URL and that's how they determine which profile page to display to you.
This is basically what #Granny commented.

How to redirect to a page using PHP in one page website

I have one a page webiste www.yahavi.com (all pages in 1 html) in which different pages are referenced using their div id (eg; #home, #contact ). After successful sign up I want to direct user to my thankyou page with id #thanku . How can i do that using my PHP script that runs after form submission.
home url: www.yahavi.com/#home
form at : www.yahavi.com/#contact
thankyou: www.yahavi.com/#thanku
I am using header (Location: /#thanku) in my PHP but this doesnt work.
Note: I dont want a complete new html page to reload.
You can use javascript on your PHP code like this:
<?php
<script> location.href="#thanku" </script>
?>
Try that. I hope this helps.
UPDATE
If you do AJAX instead, it would be best. After signup, if the user signups successfully, on the javascript file you can use location.href="#thanku".
For that, you would need to do an AJAX request in your signup form.
You can do it with just PHP. No need for JavaScript or jQuery.
Declare a global variable for storing your base path like this
$BASE_URL="www.yahavi.com/";
and redirect to the specified page like
header("Location:'".$BASE_URL."#thanku'");

Redirect using post and get the succes of the redirect

Say, I have a simple form on my website having three fields : name, password and email.
I have to get these information from the users, and keep in my database.
Then redirect to another website and send all these information using post.
I also have to know whether the user was successfully redirected to that site(HTTP STATUS 200).
Here's how I'm doing it:
For Point 1, I'm simply submitting the form.
After the data has been successfully saved in my database, I'm rendering the following form with hidden fields. This gets submitted and user gets redirected to anotherwebsite.com
<form id="form_id" action="https://www.anotherwebsite.com/form" method="POST">
<input type ="hidden" name ="name" value ="$name">
<input type ="hidden" name ="password" value ="$password">
<input type ="hidden" name ="email" value ="$email">
</form>
<script> document.getElementById('form_id').submit(); </script>
Problems:
I don't think my strategy to achieve point 1 and 2 is correct. I need a better solution. Submitting the form, then rendering a page with hidden fields and submitting it again to redirect to another site just doesn't feel right.
I have no clue to achieve the 3rd point.
Based on your question you might try this approach:
create a form with name, password, email fields in a file ( HTML ).
Submit the form to server.
On the server side get the data (including the form attribute in a variable) and save it to database.
then redirect to the given website ( using the variable you've stored in step 3 ).
You can easily know the status ( 202 or any error) using any of server side scripting language.
If you are sending the user to another website, the only way to know that the user was successfully redirected is for that website to notify you in some manner. Once the user leaves your page (and that's what a redirect is - it tells the browser "leave this URI and go to this URI instead"), the scripts on that page stop running, so they can't collect any further information.
If you just need to know that the information was submitted successfully, your script could POST the data in the background, wait for a 200 response, then redirect after the information has been submitted. But that may not meet your requirements, since you still won't know if the redirect succeeded.
Another possibility which does allow you to know whether the page on the other site loaded correctly would be to open it in a new browser window/tab instead of redirecting. This is the only way to keep your page active (and, thus, your scripts able to run) while loading another page. However, it introduces other issues, like what to do with the original page. (Leave it open in the background (likely to confuse the user) or close itself after seeing that the new URI has loaded (could cause undesirable visual artifacts as one window/tab opens and then the original one closes; destroys browser history)?)
If at all possible, having the final destination site notify you when the transaction completes is almost certainly the best way to go.
To achieve point 3 you need to use cookies if you are actually trying to implement a login-cum-membersarea system. Othewise, you simple need a redirect inside a condition statement.
my $cgi = CGI->new;
if (condition) { print $cgi->redirect('https://www.examplesite.com/file.html') }
for a general way of doing point 1-2, you can look at the tutorial here:
http://practicalperl5.blogspot.com/

Rails3 Redirect for javascript/ajax

I'm using a form with the remote => true. I have a validation that is working correctly.
My problem is when the user enters all the correct information and writes the data at this time I do a redirect to the page of the show. Instead of redirecting, it displays the html of the show in my div. How can I make a javascript redirect that will understand? I did not want to use the window.location.
Thanks...
you need to change your Update view into an update.js.erb and supply the javascript you wish to return with. i typically use an ajax flash notice as the response.
here is a good walkthrough:
http://www.whatcodecraves.com/articles/2008/12/13/rails_flash_with_ajax/

How do I preserve radio button and check box value without using cookies after the browser refresh?

I have three radio buttons and 4 check boxes.
I want to preserve the radio button and check box values after the browser refresh.
I do not want to use cookies (due to some other reason).
Could anyone please help me?
I don't think this is possible because HTTP is stateless, cookies or server side scripting provide 'state'.
You could use sessions instead.
EDIT: My bad, I read PHP and not Javascript. However I did find this link after a quick Google search. Session variables without cookies in JS
You might be able to use the hash url.
something like this (don't remember if you need to specify the name of the page as well, but I don't think so):
document.location.href = '#radio1=1&radio2=0'
The hash means it just directs things on the current page and not going to another page (and the browser updates it in the address field, so if the user reloads the page, it will still be there). Then you can read it from javascript as well and set it.
Not as good as using server side sessions, but it is an option :)
If you're using a form to trigger a new page loading you can make the onsubmit event call a javascript function to change the window location and append URL parameters that store the values of the radio buttons. When the page loads you would then read the parameter values from the URL. Something like this:
<script type="text/javascript">
function changeURL(){
var str = window.location+"?radio1=1";
window.open(str);
return false;
}
</script>
...
<FORM onsubmit="changeURL();">
<INPUT TYPE="submit" value="click me" >
</FORM>
A new facility is being developed to allow web sites to store persistent data on the client machine. Available in some browsers already this allows you to save the the radio and checkbox states and recover and restore them next time the user visits your site. For more info see here https://developer.mozilla.org/en/DOM/Storage and here http://dev.w3.org/html5/webstorage
Have some JS on the page submit all radiobutton/checkbox events to the server, and store their state in your database. When the page (re)loads, send this state as part of the HTML.

Categories