hariskar Posted June 3, 2016 Share Posted June 3, 2016 I would like to ask if this function is possible with javascript: After a member logins in my site with name eg user1, if he clicks on menu item "Results" I want him to see only his results, so go to link www.site.com/user1. A second user with name user2 if he clicks the same menu item "Results" www.site.com/user2 should open. Is that possible with javascript? How should I start? Thank you Link to comment Share on other sites More sharing options...
Ingolme Posted June 3, 2016 Share Posted June 3, 2016 You should do that with a server-side language. Whichever language you built the login system with. What happens if user 1 types www.site.com/user2 into his browser? Will it show him the results for user 2? 1 Link to comment Share on other sites More sharing options...
hariskar Posted June 3, 2016 Author Share Posted June 3, 2016 (edited) No, they have to be private. Each user should see only his results. Thank you for reply! Edited June 3, 2016 by hariskar Link to comment Share on other sites More sharing options...
Ingolme Posted June 3, 2016 Share Posted June 3, 2016 If only the user sees the page, does it make sense to have one URL for each user? Just having a single URL, like www.site.com/results, will be enough. On that page you would detect which user is logged in and display the correct information using a server-side language. 1 Link to comment Share on other sites More sharing options...
hariskar Posted June 3, 2016 Author Share Posted June 3, 2016 My site now with drupal and views works exactly like this: www.site.com/results for every user, but each user can see only his results. But now I want to rebuild my site without drupal. So I have to learn php . Link to comment Share on other sites More sharing options...
hariskar Posted July 11, 2016 Author Share Posted July 11, 2016 If someone logins, there is a redirection in login.php to results.php if($user->login($username,$password)){ $_SESSION['username'] = $username; header('Location: /results.php'); So, in results.php there is a paragraph that should be visible for not logged in users. I would like it to be replaced with the users results if the user has results and is logged in. But it should be replaced with each users own results. Of course each users should be able to see only his results. If he has no result this paragraph and is logged in should just not show. Could you please give me some hints? I thought: With session_start these in the 2 files: and put in results.php something like: <?php $results = file_get_contents($username".html");?> <script> function ChangeParagraph(par1) { if( $username->is_logged_in() { document.getElementById("par1").innerHTML = "<?php echo $results; ?>"; } } </script> ?> of course it does not work.. Thank you Link to comment Share on other sites More sharing options...
dsonesuk Posted July 11, 2016 Share Posted July 11, 2016 Why mix JavaScript and php? can't you just create if else for paragraph content and assign to variable then echo result wherever you require it, it is then not relying on JavaScript being enabled to work. PS where is function being called to display result? Link to comment Share on other sites More sharing options...
hariskar Posted July 12, 2016 Author Share Posted July 12, 2016 (edited) Thank you, this works for me <?php if (!$_SESSION['username']) {?> <p>Original text</p> <?php } elseif ($_SESSION['username']==username1) {?> <p>Text for username1</p> <?php } ?> Edited July 12, 2016 by hariskar Link to comment Share on other sites More sharing options...
hariskar Posted July 12, 2016 Author Share Posted July 12, 2016 (edited) With 3 cases it does not work as expected, if user hariskar logs in, echoed text is "hariskar text". If hariskar user logs out and test user logs in echoed text is again hariskar, which is wrong, although if I do print_r($_SESSION); the result is "testArray ( [loggedin] => 1 [username] => test [memberID] => 2 )" which is correct. Why do we not see the "test text" echoed? Thank you! <?php if ($_SESSION['username']==hariskar) { echo "hariskar text"; } elseif($_SESSION['username']==test) { echo "test text"; } else { echo "Some text."; } ?> PS. Maybe this topic should be moved in php section? Edited July 12, 2016 by hariskar Link to comment Share on other sites More sharing options...
dsonesuk Posted July 12, 2016 Share Posted July 12, 2016 Wrap in quotes 'hariskar' , 'test', in if/else condition Link to comment Share on other sites More sharing options...
hariskar Posted July 12, 2016 Author Share Posted July 12, 2016 Thank you but I have already tried it, no difference, it still shows the "hariskar text" for user test.. <?php if ($_SESSION['username']=="hariskar") { echo "hariskar text"; } elseif($_SESSION['username']=="test") { echo "test text"; } else { echo "Some text."; } ?> Link to comment Share on other sites More sharing options...
dsonesuk Posted July 12, 2016 Share Posted July 12, 2016 I don't unerstand where testArray is coming from? Echo session['username'] before if/else conditions. Link to comment Share on other sites More sharing options...
hariskar Posted July 12, 2016 Author Share Posted July 12, 2016 (edited) With <?php echo $_SESSION['username']; if ($_SESSION['username']=="hariskar") { echo "hariskar text"; } elseif($_SESSION['username']=="test") { echo "test text"; } else { echo "Some text."; } ?> for logged in user hariskar the result is 1hariskar text for logged in user test the result is again 1hariskar text So echo $_SESSION['username']; gives 1. But why? I start session with <?php session_start(); ?> in 1st line of results.php. Thank you Edited July 12, 2016 by hariskar Link to comment Share on other sites More sharing options...
hariskar Posted July 12, 2016 Author Share Posted July 12, 2016 Now for both users print_r($_SESSION); gives Array ( [loggedin] => 1 [username] => 1 [memberID] => 2 ) Link to comment Share on other sites More sharing options...
dsonesuk Posted July 12, 2016 Share Posted July 12, 2016 Looks like 'username' session is getting 'loggedin' value instead Link to comment Share on other sites More sharing options...
hariskar Posted July 13, 2016 Author Share Posted July 13, 2016 (edited) It works perfectly now, something broke in included file login.php, but now it is OK! Thank you! <?php if ($_SESSION['username']=="hariskar") { echo "hariskar text"; readfile("arthra.html"); } elseif($_SESSION['username']=="test") { readfile("results/xxx.html"); } else { ?> <p>Αν θέλετε να μπορείτε να κατεβάζετε τα αποτελέσματά σας σε μορφή pdf παρακαλώ ενημερώστε μας και <a href="/user/register">εγγραφείτε στη σελίδα μας</a>.</p> <?php } ?> Edited July 13, 2016 by hariskar Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now