G'day,
This is for my tutorial purpose.
I have 3 files
1. mlogin.htm - Takes the input from the user (login name and password). The action is set to the next file so the details can be checked.
<form id="logIn" name="logIn" method="get" action="mlogin.php">
2. mlogin.php - Takes the value from mlogin.htm using GET method. If the details match the details in XML file, the user is redirected to the next file
$musername = $_GET['username'];
$mpassword = $_GET['password'];
exit(header('Refresh:5; url=mloginsuccess.htm'));
3. mloginsuccess.htm - Displays the menu.
Now, what I'm trying to do is to show the username in the 3rd file so it's something like
Welcome, John
I do realise that I can do this using a session by changing the 3rd file to a
mloginsuccess.php
but it MUST be a
mloginsuccess.htm
I was wondering if this is possible.
Any help is appreciated :)
Suppose for a moment that you actually do want to follow your instructions to the letter. (You don't really want to do this, probably... interpreting requirements, rather than following them exactly, is a key trait of a decent software engineer.) If your requirement is that you must use a static page, you have a couple options for getting data accessible on that page. All of which require JavaScript.
Cookies
Query String
Anchor Fragment
Basically, you need to set this data in one of these three places so that you can access it with JavaScript from your static HTML page later on. To set a cookie with PHP, use setcookie(). To read it with JavaScript, use document.cookie, or one of the many snippets of code to make this easier.
To set the query string, simply do so in your redirect:
header('Location: http://www.example.com/mloginsuccess.htm?name=' . urlencode($_GET['username']));
See this question for the JavaScript needed to read the query string: How to get the value from the GET parameters?
Finally, for the anchor fragment, you can often redirect to it the same way. (However note that not all browsers are guaranteed to follow the anchor fragment part of the URL!) To read the anchor fragment, use window.location.hash.
I hope that in the end, you will choose to do none of these and keep your auth logic in a sensible place. Literal interpretation of requirements rarely leads to good code and application design. At a minimum, you can hack around the URL requirement with a rewrite rule, making whatever.html be an alias to whatever.php. The client doesn't know or care what is actually running on the server... that's the server's job. I would tell you how to write a rewrite rule, but you didn't specify which server you are using, so I'll leave that part up to you to Google.
How can you expect to use a php feature(SESSION) in a file which is not php(.HTML).
However you are allowed to use html inside a php file as php is a template engine and process the html ...refer this for for indepth
What renders the HTML?
just convert your .html to .php and
<?php>
session_start();
$_SESSION['username']=$_GET['username']
?>
<html>....<body>welcome <?=$_SESSION['username']?></body>...</html>
or however your html tags are.
Maybe you can use AJAX to load session details. For example, using JQuery,
<script>
...
$(document).ready(function(){
$.ajax({
url: "load_session.php",
success: function(uname){
$("#uname").html(uname);
}
});
});
...
</script>
...
Welcome, <span id="uname"></span>
Related
When I click on "Laxatives"
Then I should see the '/laxatives" page
For the above behat scenario how can i validate or make sure that it redirects to correct url.
For now when i run this it redirects to correct page, but if incase it does not how will i validate through script.Kindly help
when there is no big amount of links you're testing, you can use switch statement to specify expected URL for each option.
Otherwise I would suggest to create some class acting like translations, so when you request Laxatives, it will tell you, that "/laxatives" string must be present within the page URL. You can then specify this "translations" in some JSON or CSV file.
Then just use: $this->assertSession()->addressMatches($regex); where the regex will be set by switch statement or loaded by the class I mentioned.
The simplest and easiest way of achieving is this:
Given I am on 'home-page'
When I follow 'link-to-laxatives-page'
Then I should see 'Welcome to Laxatives'
So Welcome to Laxatives is a simple text which presents in /laxatives page.
Note: If there is a text which is completely unique to the page then use that otherwise use something else.
OTHER OPTIONS:
You can use getCurrentUrl() in your FeatureContext.
Use already build-in step which is in MinkContext
I have a situation like this.
<php>
<redirect the page >
<exit>
<javascript>
<redirect the page again>
I want to have javascript that basicall disables the PHP redirect. So if Javascript is enabled on the browser, the javascript redirect will work, if it disable, the PHP redirect will work. Should I just enclose the PHP code in span and make it invisible? Any ideas?
Addition ok this is not a simple redirect. the form authentication is rather odd. Register.php -> register_submit.php -> Was there an error -> yes go back to register.php (everything is javascript at this point). What I have added is PHP authentication as well so if I see javascript is not enabled, I take the user to register.php *after it does the regular checking of fields *.
PHP is a server-side technology. By the time Javascript even sees what's happened, it's too late.
Short answer, JS can't intercept/block PHP (as long as PHP is being called first).
Order of events:
Client requests page
PHP executes and generates output of page
Browser receives output
Browser begins parsing what was sent by what PHP already spit out.
Remove your PHP redirection and add this in your <head>:
<noscript>
<meta http-equiv="refresh" content="0; http://www.example.com/1" />
</noscript>
<script>
window.location = 'http://www.example.com/2';
</script>
This will redirect to http://www.example.com/1 when javascript is disabled, and to http://www.example.com/2 when it's enabled.
PHP code is executed on the server-side, while JS is client-side. So with that structure the PHP will kick in before the JS is executed. If you want JS to control PHP you need to make use of AJAX to control it.
Also enclosing PHP code in a "span" won't have any effect.
Javascript and PHP do not directly interact (exceptions apply, don't worry about them now :D). The best way to implement this type of interaction between these two disparate languages is to use the query string or cookies.
I think there may be some confusion here about when and how PHP is executed as opposed to when and how javascript is executed. Think of PHP as the factory - the goods are physically produced there. Think of your server as the loading dock, the internet as the shipping company. Your browser is the store, HTML is the shelves; Javascript is the window decorations on the store that sells the merchandise. The window decorations have no affect on the production, the factory can make some window decorations, but it doesn't use them, it just ships them right along with the merchandise for the store to use. PHP is the factory, javascript is the decoration. There are some problems with taking this analogy too literally, but there it is in a nutshell.
You can make the PHP redirect conditional on the presence or absence of a specific query string variable:
<?php
// redirect if $_GET['no_redirect'] is NOT set. Reverse the true/false to invert this rule
$do_redirect = (isset($_GET['no_redirect']) === false ? true : false);
// perform the redirect, if required
if ($do_redirect === false)
header('Location:http://mydomain.com');
?>
Javascript:
window.location = 'http://mydomain.com/?no_redirect=1';
EDIT If you're trying to detect if javascript is enabled, then the best way is for javascript to set a cookie if it is enabled. PHP can then check for this cookie, and if it isn't found then you'll know that javascript didn't get a chance to set it, so it must be disabled (or the user edited their cookies).
Take a look at some code snippets for dealing with cookies in javascript, and check out the documentation for dealing with cookies in PHP.
is it possible to store a variable through javascript so that I can read it in php using $_REQUEST['variable_name'].
for eg.
let's say i have
$adcategory=$_REQUEST['category_id'];
somewhere in the php page, and I would like to pre set that "variable" somewhere before that in javascript, so that it could be read through php.
Does this make any sense? Is this possible?
Thank you for your time!
Andrej
It's less of a technical problem, more of how you structure your code and interaction between the PHP backend and in-page Javascript.
To get the $category_id variable into Javascript, the typical approach is:
<?php print "<script>category_id = $category_id;</script>";
To have your Javascript code send a catid back to a PHP page:
$("#id").load("page.php?category_id="+category_id);
This would ping it back to PHPs $_REQEUST[] array. But the question is why you need the variable available in Javascript first.
Does this make any sense? Is this possible?
No and no :)
PHP executes on server side before anything else, Javascript in the browser. The only way to do this in JavaScript would be to manipulate the form that makes that request before it gets submitted.
Assuming you are using Apache:
Try setting the default in a file (called "my_vars.php" in this example), then in your .htaccess file:
php_value auto_prepend_file /absolute/path/to/my_vars.php
For the js side, take the value of what you set in that file at page load.
<script type="text/javascript">
var = <?=$what_i_set_in_my_vars?>;
</script>
Research:
http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=php_value+auto_prepend_file
I have found that auto prepend is good if you want to use a sort of settings file for storing stuff like this, IMHO. That way you don't need to jump through hoops, the draw back is that it will be included on each page.
Does this make any sense? Is this possible?
Yes, you need to bind it to the request string.
If you're calling foobar.php, change the onsubmit behavior of the form and make it:
'foobar.php?category_id=' + your_javascript_category_id_value
I'm building a proxy and am trying to deal with a page that uses javascript. The page has a button like this:
<input type="submit" ...cut this out... onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(...cut this out...)) />
When I click this button from my proxy the URL is rewritten to look like this (notice the javascript code inserted here):
http://domain.com/proxy/index-new.php?q=https://proxiedomain.com/javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(...cut this out...))
I'm not sure how I can handle this in my proxy server. When I don't use a proxy the headers are sent to a completely different page (the URL doesn't include this javascript). Can anyone give me any hints as to what I should look into or read to understand this problem better? From what I understand so far, I need this javascript to be executed (which would require a cient browser).
Any link that points to javascript:... will run JavaScript but not necessarily load a page.
I would leave these links alone, and instead ensure that the form action URL is set to your proxy, and any location.href = 'http://www.example.com/fully_qualified_urls'; are swapped for the proxy URL.
e.g. a simple RegEx replace of "OLD_URL" for "NEW_URL" (accounting for any HTTP vs. HTTPS protocol differences) should suffice for the most part.
Note: I'm aware it isn't "simple", but trying to inspect a javascript: based "link" to modify its behavior will be very awkward.
I have a quick question i hope you guys can answer, i've got a search system that uses POST to do searches, now i want to track queries using Google Analytics but it requires using GET url parameters to pull parameters out the URL, what i don't want to do is rewrite the entire search system to use GET instead of POST. Is there any way around this? I was thinking maybe i can make a GET call to a new page from the page that recieves the search POSTs, but i don't want it to redirect, i merely want it to "hit" the url without actually redirecting?
Is this possible?
Any other solutions would also be appreciated.
Thanks for the help
You can specify an abritrary URL when you add your GA code. For example, all our different checkout pages go through validate.php, so this is the URL that the person would see, however, we put in some extra code to give a specific tracking URL to google.
For example:-
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._setDomainName("example.com");
pageTracker._trackPageview("/checkout/login/");
} catch(err) {}
</script>
Would make google track this as being /checkout/login/ even though the page in the browser actually shows /validate.php
You can output (as we do) this page variable from different PHP variables
$searchterm = $_POST['search'];
echo 'pageTracker._trackPageview("/search/' . urlencode($searchterm) . '");';
Sure, use the apache mod_rewrite module to create a fancy, seo friendly url and pass the user keywords in the url.
Something like "www.yoursite.com/search/what+a+user+searches+for/"
In a .htaccess file you create a rule
RewriteRule ^search/(.*)/$ /search.php?keywords=$1
You're orignal script keeps working with your postvalues and you provide an URL with GET variables. With explode("+", $_GET["keywords"]) you can extract the searchvalues.
With the array $_REQUEST you can access all request parameters GET and POST.
The only way you will be able to do this, is re-set the forms method to GET and just changed the $_POST requests to $_GET
Thats not such a huge change?
You should be able to do that with HTTPRequest:
http://php.net/manual/en/class.httprequest.php
You can just alter your Google Analytics code - see Tracking Custom Variables