JackCode Posted January 6, 2014 Share Posted January 6, 2014 <?php if(isset($_SESSION['id']) && $_SESSION['id'] > 0) { echo "<li><a href='profile.php?=$_SESSION['id']'>My profile</a></li>"; } Hello, my quotation marks don't link up to the right ones, anyone know how to fix this issue? the red ones need to match up Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 Wrap $_SESSION['id'] in curly braces echo "<li><a href='profile.php?={$_SESSION['id']}'>My profile</a></li>"; Or concatenate echo "<li><a href='profile.php?=".$_SESSION['id']."'>My profile</a></li>"; Quote Link to comment Share on other sites More sharing options...
JIXO Posted January 6, 2014 Share Posted January 6, 2014 You can also escape : echo "<li><a href='profile.php?=$_SESSION[\'id\']'>My profile</a></li>"; Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Thank you very much both. I have done this and the error has gone, but now so has the link... Any ideas? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) You can also escape : echo "<li><a href='profile.php?=$_SESSION[\'id\']'>My profile</a></li>"; No, that wont work either. When using complex variable name like an associative array within a sting you need to wrap it within curly braces. You cannot escape the quotes for the variable The only time you'd escape quotes would be for the HTML, eg echo "<li><a href=\"profile.php?={$_SESSION['id']}\">My profile</a></li>"; // ^---- escape double quotes ----^ Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Thank you I have solved the single/double quote problem but now my link has disappeared. Can anyone explain why? Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 Thank you I have solved the single/double quote problem but now my link has disappeared. Can anyone explain why? Thanks My code suggestions should work fine. What di d you change your code to? Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 (edited) <?php if(isset($_SESSION['id']) && $_SESSION['id'] > 0) { echo "<li><a href='profile.php?=$_SESSION{['id']}'>My profile</a></li>"; } ?> The link has now completely disappeared but in my IDE it says there's no errors. Edited January 6, 2014 by JackCode Quote Link to comment Share on other sites More sharing options...
Dowlat Posted January 6, 2014 Share Posted January 6, 2014 (edited) Thank you I have solved the single/double quote problem but now my link has disappeared. Can anyone explain why? Thanks Your conditional expression is not returning true. Print the contents of $_SESSION and check if it has been properly initialized in your code. Also Jack, make it good habit to always encapsulate your code with the CODE TAGS provided in the editor. Edited January 6, 2014 by Dowlat Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Your conditional expression is not returning true. Print the contents of $_SESSION and check if it has been properly initialized in your code. Can you show me how to return it true and initialize. Sorry I am new to PHP. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) <?php if(isset($_SESSION['id']) && $_SESSION['id'] > 0) { echo "<li><a href='profile.php?=$_SESSION{['id']}'>My profile</a></li>"; } ?> The link has now completely disappeared but in my IDE it says there's no errors. The curly braces need to go around the whole variable, as {$_SESSION['id']} echo "<li><a href='profile.php?={$_SESSION['id']}'>My profile</a></li>"; // ^---- brace ----^ Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Done that, makes no difference. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) Well that is the correct syntax, If your link is not appearing your code might be outputting invalid HTML which is causing the link to not appear. When you run your PHP code check the HTML source code via Right click > view source. Can you spot an HTML error? Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Well that is the correct syntax, If your link is not appearing your code might be outputting invalid HTML which is causing the link to not appear. When you run your PHP code check the HTML source code via Right click > view source. Can you spot an HTML error? There is definitely not an error because I tried it in a separate document named test.php and nothing appeared. Basically what I'm trying to do is create a universal link from a page to my profile.php page. Any advice on what I can use alternatively? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 There is definitely not an error because I tried it in a separate document named test.php and nothing appeared. By this do you mean you get a completely blank page? If you do, then check your servers error log for php errors. Or turn error reporting on at the top of your PHP script. ini_set('display_errrors', 1); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 By this do you mean you get a completely blank page? If you do, then check your servers error log for php errors. Or turn error reporting on at the top of your PHP script. ini_set('display_errrors', 1); error_reporting(E_ALL); No I don't mean that I mean there is no errors with the HTML because I tried the PHP in a page with no other code and when I tested it there still was no link so there must be an error with the PHP code causing it to not appear. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 Can you tell me how you are testing the code. The code I have posted is free of any PHP/HTML errors and will output a link just fine. Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Using a web server: my website is backupransack.freeoda.com If you look on the home page then game page the home page is missing the 'my profile' link. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) Your link is visible! The problem is $_SESSION['id'] value has not been outputted, most probably due this variable not being defined before hand, has no value or you have not started the session. What is the output of <?php printf('<pre>%s</pre>', print_r($_SESSION, true)); ?> Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Dowlat Posted January 6, 2014 Share Posted January 6, 2014 (edited) Using a web server: my website is backupransack.freeoda.com If you look on the home page then game page the home page is missing the 'my profile' link. Jackcode, The only way the profile link will be printed entirely is if the conditional statement returns (BOOL) True. If your link is visible then thats a different story, your problem lies with the html. If your conditional statement is true, then the ID should print a number more than 0, and it is set. so I don't see how your url could be empty. Edited January 6, 2014 by Dowlat Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Ch0cu3r I have sent you a message with my code. Please reply? Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 Your link is visible! The problem is $_SESSION['id'] value has not been outputted, most probably due this variable not being defined before hand, has no value or you have not started the session. What is the output of <?php printf('<pre>%s</pre>', print_r($_SESSION, true)); ?> nothing. Quote Link to comment Share on other sites More sharing options...
Dowlat Posted January 6, 2014 Share Posted January 6, 2014 nothing. Post your code, where you are setting the sessions. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 (edited) nothing. You was logged into your site when you ran my code? Edited January 6, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
JackCode Posted January 6, 2014 Author Share Posted January 6, 2014 (edited) I put it under my link and when I log in it says: array [username] => Jack [id] => 2 Edited January 6, 2014 by JackCode Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.