Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP sessions

Options
  • 02-12-2010 6:10pm
    #1
    Closed Accounts Posts: 2,696 ✭✭✭


    Can i save a webpage as html and store session variables at the same time or do i need to format page as php?

    I would like to use html pages and php pages within same session - is it possible??


Comments

  • Registered Users Posts: 489 ✭✭Pablod


    I have only used php once for a very basic logon, so am no expert whatsoever.

    My main website is all html, but for carrying variables from a logon form through to other pages, all the relevant pages where you require the session - have to be php pages.

    Hope this helps


  • Registered Users Posts: 894 ✭✭✭Dale Parish


    You must use a PHP page.
    You can put HTML into a PHP page easily.
    Start the page off by naming your session session_name("something");
    and then calling session_start();

    Then in the HTML area you can use the sessions where you like (assuming they have been assigned by something such as a HTML form).

    i.e.
    <body>
    <p>Hello <php echo $_SESSION['username']; ?>.</p>
    </body>
    


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    But i dont want to use a php page

    it seems i can login to html page > process the login with a php page > then redirect to html form page, submit the form data, and all along hold on to session variables while only starting session on php process login and process form pages

    i would still like to use php to output to html file but cant seem to do it


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Your webserver looks at file extensions to see which is the best method to parse them. If it sees a html page, it assumes it is only html. So it won't parse it as PHP, but rather as text. You'll either get a blank screen, or the PHP code printed to the screen.

    You can configure your web-server to parse html files as PHP - Apache for example can do this, but it isn't recommended. Why would you not want to use a php extension?


  • Registered Users Posts: 139 ✭✭Bald? er, dash!


    What purpose is there to store the session variables at all? What are you trying to do exactly?


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    i tried just adding <?php ?> to beginning and end of code page and saving as php but i get xml error

    have assignment due in next week and dont really have time to convert


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    What purpose is there to store the session variables at all? What are you trying to do exactly?

    I'm assuming to have a state to show that the user has logged in.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Just because you have a session don't preclude you from using a HTML page, it's simply that the HTML page won't be aware of your PHP session.

    When you create the session, you can usually assume that the only thing on your webserver that has access to that session is the PHP module - thus only PHP pages will be able to read and write information from the session.... so if you want to print the user name, you'll have to have a PHP page. But if you just want to send out a generic login page using HTML, you can do that too.


  • Registered Users Posts: 2,781 ✭✭✭amen


    I think the what the op wants to do is have a php page that outputs html with some php session information such as the user name in the html.

    In that case you create a page with php tags on it (note I don't know php)
    and on the page your write your normal html. When you want to access a php variable or session value you can put some php in the page which will inject the require value at that point.
    <php>
    do some stuff
    </php>
    <html>
    <body>
    Thanks for logging in <php> session("UserName") </php>
    </body>
    </html
    


  • Registered Users Posts: 912 ✭✭✭chakotha


    If you want files with the .html extension to be processed for PHP on the server and your web server is Apache you could edit the Apache configuration file httpd.conf on the line for AddType application/x-httpd-php.

    Change it to this and reboot:
    AddType application/x-httpd-php .php3 .php .html

    On my server the config file httpd.conf is located in /usr/local/etc/httpd/conf - YMMV


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    httpd.config empty and is located on /etc/apache2 - using LAMP on virtual box

    can i have the full line of text so i can try adding it


  • Registered Users Posts: 894 ✭✭✭Dale Parish


    It should be in the config DIR. That is the full line; I don't see why you don't just save your HTML pages as PHP pages with the HTML code in them :confused:


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    It should be in the config DIR. That is the full line; I don't see why you don't just save your HTML pages as PHP pages with the HTML code in them :confused:

    This.


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    ok i brought mohammed to the mountain instead

    previous validation was xhtml 1.0 transitional - where do i go with validating now?


  • Registered Users Posts: 489 ✭✭Pablod


    john47832 wrote: »
    ok i brought mohammed to the mountain instead

    previous validation was xhtml 1.0 transitional - where do i go with validating now?

    Leave it as xhtml 1.0 transitional
    If you create a new PHP or save an existing HTML as a PHP the you'll get the same result on validation


  • Registered Users Posts: 981 ✭✭✭fasty


    chakotha wrote: »
    If you want files with the .html extension to be processed for PHP on the server and your web server is Apache you could edit the Apache configuration file httpd.conf on the line for AddType application/x-httpd-php.

    Change it to this and reboot:



    On my server the config file httpd.conf is located in /usr/local/etc/httpd/conf - YMMV

    A small note about this. Doing so makes ALL html pages get interpreted by PHP. Usually not a problem, but something to keep in mind.


  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,174 Mod ✭✭✭✭Jonathan


    fasty wrote: »
    Usually not a problem, but something to keep in mind.
    Generally only a problem on very high load sites where you want a static homepage etc.


  • Registered Users Posts: 2,739 ✭✭✭MyPeopleDrankTheSoup


    fasty wrote: »
    A small note about this. Doing so makes ALL html pages get interpreted by PHP. Usually not a problem, but something to keep in mind.

    lol, i love this stock answer from 2000. if you havn't noticed it's 2010, find me a decent size website with ONE webpage that is FULLY static.


  • Registered Users Posts: 981 ✭✭✭fasty


    Ha, I was just saying guys and I did mention that it's unlikely to be an issue, just something to keep in mind!


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    ok lads try and stay with it here - we nearly there

    if i add below xml to my code then php doesnt seem to like it - where do i put it?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


  • Advertisement
  • Registered Users Posts: 981 ✭✭✭fasty


    <?php
    session_start();
    // do stuff with session or get data from database etc here
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    	<head>
    		<meta http-equiv="content-type" content="text/html; charset=utf-16" />
    		<title>Test</title>
    	</head>
    	<body>
    		<?php echo "output some php related stuff like this from within your HTML"; ?>
    	</body>
    </html>
    


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    sorted - new issue


    i have variable to store form data

    // takes 2 values (1 2)
    $semester = ($_POST);
    $N = count($semester);


    //outputs two values as 1 2
    for ($i =0; $< $N; $i++) {
    echo ($semester[$i] . " ");
    }

    I need to query database using those two values

    example

    SELECT a.module, b.year FROM table1 as a, table2 as b
    WHERE a.module_id=b.module_id
    and b.semester IN ($semester);


    any simple way of doing this??


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    i got it - implode


  • Registered Users Posts: 981 ✭✭✭fasty


    This is all stuff that shows up in a million examples on Google. It feels like I'm doing your homework. Last piece of sample code below from me.
    <?php
    $connection = mysql_connect("your_sqlserver_ip", "your_username","your_password") or die(mysql_error());
    mysql_select_db("your_db_name", $connection);
    
    $query = "SELECT a.module, b.year FROM table1 as a, table2 as b WHERE a.module_id=b.module_id and b.semester IN ( ." mysql_real_escape_string(implode(",", $semester)) . ")";
    $result = mysql_query($query, $connection) or die(mysql_error());
    
    while ($row = mysql_fetch_assoc($result) {
    	// do something with $row
    }
    
    mysql_free_result($result);
    mysql_close($connection);
    


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    fasty wrote: »
    This is all stuff that shows up in a million examples on Google. It feels like I'm doing your homework. Last piece of sample code below from me.

    Id rather you didnt bother if you are going to be patronizing about it


  • Registered Users Posts: 981 ✭✭✭fasty


    No problem. Good luck with the assignment.


Advertisement