PHP - How To Handle Log-in Routing??
Okay, so now I need help evolving my Log-In system...
Up until now, the only place a user could log-in was on a given Article page if they wanted to add a comment to the Article. To accommodate that feature, I was setting the "Return To Page" only in my "article.php" script like this... Code: [Select] $_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '?title=' . $articleTitle; However, now I want to expand things. I'm growing increasingly confused about how to manage where to route people when they long-in?! 1.) Sometimes a user will Log-In and want to return where they were at (e.g. index.php, article1234.php) 2.) Sometimes a user might want to continue down a path (e.g. Checking Out, Registering for a Workshop, Sending a Message) Is there some kind of strategy to handle this? (nothing over complex, but there must be some Best Practices that work?! Debbie Similar TutorialsHi I relatively new to MVC and trying to understand it all by coding my own MVC framework. I already have coded my (own variant) MVC application: a router calling controllers each creating CRUD(L)* forms (views) onto separate db tables. Now I want to create a dashboard with multiple forms each modifying a separate database table. I already have the ability for one controller to call another controller ad infinitum. However when I press "modify" on one of the forms I do not know how to route (call the router) to rebuild the dashboard restoring all the other forms (ie other controllers) to the same state they were in when my target form requested a modification. Basically I don't know how to go about routing in a hierarchical or nested MVC framework. Should I design a complex (non-linear tree-like) URL request that captures the state of my dashboard and have a very complex router that can decode it? Should each action be idempotent or should I use state in the session to make the request URL easier? *CRUD(L) a view with different forms for CRUD and L(ist) operations each invling different controller methods. I have tried searching under "HMVC" (but that seems to mean "modules") and "nested MVC" but for both terms I mostly get referred to established frameworks. Cheers GAJ Hi guys, I am developing an MVC application and need some advice about how to set up url routing. Currently I can map a uri to modules/controllers and actions. I though I might leave the routing to last but it seems like that might have been a bad idea. Initially I though I might implement a Zend style routing approach and I still want to do this, but I am lost for thoughs on how I might go about this, I have looked at the zend router classes and understand the principle but cant seem to implement it in my own mvc. So far I have this, $router is an instance of a router class. mapRoute takes a route name and a route object as parameters. The name will be used for convinience. The second parameter takes a route instance which currently takes two parameters, where param1 is a route pattern and param2 is deafult values for when a uri macthes the route pattern. So if a user types http://example/user/23, the router should map to user controller, profile action. The id, which is 23, will need to be injected into a request object. Finally the router object will be added to a front controller instance, so the front controller object can gain access to all the routes. Code: [Select] $router->mapRoute("user", new Route( "user/<id>", array( "controller"=>"user", "action"=>"profile" ) ) ); If any one has any ideas please do share, im not after code, unless you want to supply some, I just need some advice to stream line the whole process or what the best way might be. Any help will be greatly appreciated. Hey all, I'm using cakephp and it asks me a question and I'm not sure what to put in, because I don't necessarily know the consequences of what I put in: Code: [Select] Would you like to create the methods for admin routing? (y/n) [y] > y You need to enable Configu :write('Routing.admin','admin') in /app/config/core.php to use admin routing. What would you like the admin route to be? Example: www.example.com/admin/controller What would you like the admin route to be? [admin] > Thanks for response Creating a basic MVC framework. I may confuse myself while asking this, so bare with me if this is confusing for you also... As far as i understand, the URLs are in the following format: www.example.com/controller/method/args My goal here is to not show 'method' in the URL if it's not necessary. For example, www.example.com/contact instead of www.example.com/contact/view. This is OK. I can accomplish this. But problems arise when I want to pass arguments in the URL. Say I have the URL: www.example.com/users <----- this would by default, user the controller 'users' and call default method, 'view'..just list all users. But say i want to view a user.. John. www.example.com/users/john How can I make my script know that 'john' is an argument, and not a method? The only way I can think of this is if I always have a method in the url .. like www.example.com/users/view/username/john As of now, my script breaks up the URL at / and the first element in the array is is the controller. It calls the controller with the other arguments. so www.example.com/users/john. The script would do (simplified) $controller = 'users' $controller = new $controller('john') the 'Users' class would then see if 'john' is a method. If it is, call it, if not, then call the default method (view) with john as argument... but what if I have a method called 'edit' and someone's username is edit? then www.example.com/users/edit would be a problem. So my guestion to you guy is... how do you deal with this issue? If you have www.example.com/users/edit, how do you let the script know that 'edit' is someone's username and now a method to call? Do you pass the method in the url like so www.example.com/users/edit/edit ? Or if you have worked with a framework (Zend, cakePHP, Symfony, CodeIgniter, etc), how does the framework handle it? Ideally, i would like to be able to have www.example.com/users/john instead of www.example.com/users/view/id/john im tying to design and make a routing system for my mvc framework but im wondering the best way to do this if anyone could help on how the design pattern would be. im thinking i would have a route config file which would contain all the routes for each file routes.config <?php $routes = array('news' => 'news/index'); ?> then the routing class which would get the correct route depeding on the request class and get the dispatch class to load the controller and action? if anyone could help on the best way to do this that would be great...thanks This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=332538.0 In a project that I'm working on I can specify routing rules, which is somewhat similar to mod_rewrite but in PHP. It's currently set up to use full regular expressions, but it's kind of overkill. I'm trying to convert it to use routing rules similar to some of the php frameworks I've seen. The code I've written up below is working, and while it's unlikely that I would need anything more complex, I'm wondering if anyone would like to comment, offer suggestions, or offer criticisms. This little piece of code is just a part of a bigger routing class, but this is the code that I'm concerned with. Thanks.
<?php $cfg['routes'] = [ 'cars/(:any)/(:any)/(:num).php' => 'cars/$3/$1/$2', 'default_route' => 'cars/index', 'trucks/parts?year=(:num)' => 'parts/trucks/$1', 'vans/articles(:any)' => 'articles$1' ]; $uris = [ 'cars/toyota/tercel/2014.php', # /cars/2014/toyota/tercel 'default_route', # /cars/index 'trucks/parts?year=2014', # /parts/trucks/2014 'vans/articles?p=42342' # /articles?p=42342 ]; $i = 0; foreach( $cfg['routes'] as $k => $v ) { $k = '|^' . preg_quote( $k ) . '$|uiD'; $k = str_replace( [ '\(\:any\)', '\(\:alnum\)', '\(\:num\)', '\(\:alpha\)', '\(\:segment\)' ], [ '(.+)', '([[:alnum:]]+)', '([[:digit:]]+)', '([[:alpha:]]+)', '([^/]*)' ], $k ); echo '<br />' . $uris[$i] . '<br />' . $k . '<br />'; if( @preg_match( $k, $uris[$i] ) ) { echo preg_replace( $k, $v, $uris[$i] ) . '<br /><br />'; } $i++; } Routing issue: Cannot see a wireless router from a wired router on the same network. I have a very simple home network with a broadband connection from my ISP coming into the house with their 4-port modem/router (192.168.1.1) on it. From there I have 2 ethernet connections, one each to a wired router(192.168.1.7) and a wireless router(192.168.1.2) The wired router has one connection to my Debian Squeeze PC (usually 192.168.1.24) the wireless router has 2 or 3 connections to a PC and a tablet (usually something like 192.168.1.100 & 101... etc) The internet from my PC (192.168.1.24) is working fine through the wiired router, and the PC & tablet on the wireless router are working fine too. My problem is that I want to be able to 'see' the wireless router from my PC and I cannot connect to it or 'see' any of the connected devices. Maybe I am misunderstanding how this networking thing should work, but I think it should be possible to do this. Can anyone help me please? a) I have turned off iptables # iptables -F # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy DROP) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain logdrop (0 references) target prot opt source destination b) ifconfig -a eth1 Link encap:Ethernet HWaddr fc:75:16:e1:b8:13 inet addr:192.168.1.24 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::fe75:16ff:fee1:b813/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9123 errors:0 dropped:0 overruns:0 frame:0 TX packets:9696 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1100294 (1.0 MiB) TX bytes:844749 (824.9 KiB) Interrupt:20 Base address:0xde00 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:2740 errors:0 dropped:0 overruns:0 frame:0 TX packets:2740 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:247076 (241.2 KiB) TX bytes:247076 (241.2 KiB) b) I have added routes(I think) to the other router from my PC # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.1 192.168.1.7 255.255.255.255 UGH 0 0 0 eth1 192.168.1.2 192.168.1.1 255.255.255.255 UGH 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 0.0.0.0 192.168.1.7 0.0.0.0 UG 0 0 0 eth1 # arp -an ? (192.168.1.7) at 00:14:6c:0b:1d:da [ether] on eth1 ========================================== from the other end, the wireless router when I connect a wired connection to a laptop I get the following similar picture (the broadband router) # ping 192.168.1.1 PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data. From 192.168.1.107 icmp_seq=2 Destination Host Unreachable From 192.168.1.107 icmp_seq=3 Destination Host Unreachable From 192.168.1.107 icmp_seq=4 Destination Host Unreachable ^C --- 192.168.1.1 ping statistics --- 6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5009ms pipe 3 (the wireless router) # ping 192.168.1.2 PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. 64 bytes from 192.168.1.2: icmp_req=1 ttl=64 time=0.378 ms 64 bytes from 192.168.1.2: icmp_req=2 ttl=64 time=0.381 ms 64 bytes from 192.168.1.2: icmp_req=3 ttl=64 time=0.452 ms ^C --- 192.168.1.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1998ms rtt min/avg/max/mdev = 0.378/0.403/0.452/0.041 ms # arp -an ? (192.168.1.1) at <incomplete> on eth0 ? (192.168.1.2) at b0:48:7a:67:ea:7a [ether] on eth0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0 0.0.0.0 192.168.1.2 0.0.0.0 UG 0 0 0 eth0 # ifconfig eth0 Link encap:Ethernet HWaddr 00:16:d3:bb:64:09 inet addr:192.168.1.107 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::216:d3ff:febb:6409/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5858 errors:0 dropped:0 overruns:0 frame:0 TX packets:5491 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7093094 (6.7 MiB) TX bytes:666534 (650.9 KiB) Interrupt:17 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:121 errors:0 dropped:0 overruns:0 frame:0 TX packets:121 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:10032 (9.7 KiB) TX bytes:10032 (9.7 KiB) Edited by Zane, 28 December 2014 - 01:06 AM. So i got a table that looks somthing like this: File Name | Download | Suggested Catagory | Select Catagory | Approve -------------------------------------------------------------------------------------- test | Download | blah blah blah | Option menu | yes or no option menu test | Download | blah blah blah | Option menu | yes or no option menu test | Download | blah blah blah | Option menu | yes or no option menu what I want to do is in the file that processes this it only looks at the ones that have had the "approve column" submitted as a yes. Next it will to a mysql entry into verrified notes. Problem is with X amount of entries in that form I dont know how to look at only the ones that have said yes. Do I create a new form for each entry or put them all as one entry and after this what do I do next? So I'm sitting at my desk scratching my head on how to do a task where I pull information form a database (that i know how to do) but then i need to see if two variables equal a string and if they do count out how many times that two variables equal there two string checks and out put the counted number ? this is what I have thought of but dose not to work because I cant figure out a way to code it? $dbPull = mysql_fetch_array($result) if( $dbPull['Foo'] == 'string1' && $bdPull['Bar'] == 'string2') this is were I get lost I'm looking for (how many times in the DB dose String2 show up in the array were the above, IF statement is true ) any ideas would be most appreciated Is there a "PREFERRED METHOD" to handling telephone numbers gathered by a form? I'm torn between ideas. I need to collect the numbers, and want to be able to view them as (123)555-1212. Should I collect them as a 10 digit ONLY string, and strip them into area code, parenthesis, and hyphenated LATER? Or gather them as 3 seperated input boxes posted to the DB accordingly? Obviously, entries will have REQUIREMENTS and error messages, so I am focused on the methodology here. Thanks. I was wondering if there's any common method for user management. I'm not talking about basic stuff like DB management stuff I'm talking about registering new users with captcha, validating email addresses, password resets, etc. I can work all this stuff out by hand but it seems rather large and very repetitious so I was wondering if there's guides out there to develop this or a common script? How do I make my constructor work when I don't pass in an argument. That is, it should efault to null. Code: [Select] class Something{ private $type=null; public function __construct($t){ $this->type = $t; } public function setSomething($t){ $this->type = $t; } public function getSomething(){ if (is_null($this->type)) { return "None for you!"; } else { return "Something is " . $this->type . "."; } } } TomTees Hi all. I'm new to PHP language and I'd like to know which is the best way to handle the user login. With cookies or with sessions? Do you know some example on the web that I could inspire on? I just know how to do it in both way, I just wanna know which is the best way and like to read some code written by a pro. I have an application where things are requested/computed/shown using either Metric or English systems.
I am thinking that the best way to handle this is to store units in one system, i.e English (as I am in United States), and to only convert them to Metric for the View, when it is being requested.
And if Metric parameters are being inputted through a form, then convert them to English on input.
So in a word
* store, compute, anything back-end, do it in English system.
When anything Metric shows up, then
* convert-on-output to Metric
* convert-on-input from Metric
Does that sound like a good plan?
Are there any other equally or better-than plans to deal with systems?
Dusting off my PHP skills after some time! How would I do this... Code: [Select] IF form was submitted THEN do some PHP stuff and do not show the form again ELSE display the form for the first time Is it as simple as wrapping my HTML inside an If-Then-Else? Debbie I have just completed a modest way for Users to post Comments on Articles my website. However, the thought occurred to me today, that I haven't given Users the ability to Preview or Edit their comments after submittal. What is the best way to store info on a Comments Form so you can re-display it for Preview/Editing?? (I'm not really sure how the workflow would work for this?!) Do I capture the Comments Form Data and store it in a Session? Or in my Database? Or do something else? Security is always a concern of mine too!! Thanks, Debbie Hi, New to coding so pls bear with me.... I am running into a continual issue that wonder if there is a better way to handle it. When writing code that handles GET variables that have not been set I use the isset function with if statements to handle it, but my code looks like it is getting sloppy and I am having to repeat it a lot - for example: Every time I simply want to call a class / function that I want to pass avGET i have to do the following: Code: [Select] if (isset($_GET["filter"])){$theFilter = $pageination->filter($_GET["filter"]);} else {$theFilter = $pageination->filter();} Here is a copy of the class I have created (note sure if it works yet as not fully tested it) Code: [Select] class pageination { private $recordLimit=100; public function filter ($filter="") { if ($filter!=""){ $theFilter=" AND ".$filter; return $theFilter; } else { return $theFilter=""; } } private function pageCnt ($pageCntRst) { $pageCnt=intval($results[$pageCntRst]); return $pageCnt; } public function pageNmLnk () { echo 'Page: <a href="?pageStartListings=0&filter='. $_GET['filter'].'">1</a>'; } //This loops through every 100 pages public function pageNmLoop ($pageCnt) { for ($n=1; $n<=$pageCnt; $n++){ if ($n > $recordLimit) { break; } if ($n*$recordLimit==intval($_GET["pageStartListings"])){ $stylePg = 'style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding:3px; "> ' . $n+1 . '</a>'; } else{ $stylePg = ''; } echo '| <a href="?pageStartListings=' . $n*$recordLimit . '&filter=' . rawurlencode($_GET["filter"]) . ' " ' . $stylePg; } } } $pageination = new pageination (); ?> I use the GET a few times in the code and I have to write a lot of code around the isset and if statements to handle it all the time - thinking there must be a better way to do this or a function that can be built to test it? Any help is grateful in advance. I have 2 PHP scripts : opendb.php and closedb.php In opendb.php I have this code :
<?php
In closedb.php I have this code :
<?php When I run opendb.php I get the expexted message "DB Open." But when running the closedb.php , I get an error 500. I was hoping to be able to pass the Handle, needed to operate in my database, between PHP scripts, by pushing it into $_SESSION['dbhandler'] , and retreiving it in other scripts. Is this possible ? Regards,
Martin Hi guys On the edit profile page on my website, im trying to get is so that when the user first opens the page, if the database field reads "1" the checkbox is checked. However if the user unchecks it and makes a mistake on the form which returns an error, the checkbox remains unchecked. Also if the user unchecks the text box, the 1 in the database is changed to a 0. And vise versa. The only way i have got it to work succesfully is by using the following: <?php $test = $_POST['test']; $submit = $_POST['submit']; if (!isset($test)) { $test="0"; } else { $test="1"; } if ($submit) { mysql_query("UPDATE profiles SET test='$test' WHERE username='user'"); echo "ok"; } ?> <html> <form action="test.php" method="POST"> <input type="checkbox" name="test" <?php if ($submit) { if ($test=="1") { echo "checked='checked'";}} else { if ($row['test'] == "1") {echo "checked='checked'";}} ?> /> <input type="submit" name="submit" /> </form> </html> My problem is, i have about 60 check boxes on the page, and i dont really want to write the code below for all of them: if (!isset($test)) { $test="0"; } else { $test="1"; } Does anybody know a better way to do this so i dont end up with 420 lines of extra code? Many thanks. |